Android app option menu

【Android app option menu】高斋晓开卷,独共圣人语。这篇文章主要讲述Android app option menu相关的知识,希望能为你提供帮助。

Main Activity java
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.more, menu);
return super.onCreateOptionsMenu(menu);
}

//此重载旨在使能MENU中的ICON显示(4.0之后显示不了option menu icon)
@Override
public boolean onMenuOpened(int featureId, Menu menu) {
if (menu != null) {
if (menu.getClass().getSimpleName().equalsIgnoreCase("MenuBuilder")) {
try {
Method method = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
if (method != null) {
method.setAccessible(true);
method.invoke(menu, true);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

return super.onMenuOpened(featureId, menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.more_menu1: {
...
}
break;
case R.id.more_menu2:
break;

}
return super.onOptionsItemSelected(item);
}

  menu resouce file  
< ?xml version="1.0" encoding="utf-8"?>
< menu xmlns:android="http://schemas.android.com/apk/res/android">

< item
android:id="@+id/more_menu1"
android:icon="@drawable/ic_event_24dp"
android:title="资产统计"
> < /item>

< item
android:id="@+id/more_menu2"

android:icon="@drawable/ic_code_24dp"

android:title="代码跟踪"
> < /item>

< /menu>


























































    推荐阅读