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>



Android app option menu

上一篇:MySQL 子查询与多表联合查询


下一篇:mysql学习笔记