android menu菜单的动态隐藏
先从ADW主界面按menu按键开始,涉及以下3个方法:
1、onCreateOptionsMenu(Menu
menu)
2、onPrepareOptionsMenu(Menu
menu)
3、onOptionsItemSelected(MenuItem
item)
分别解释一下:
方法名 | 触发时间 | 如何更新 |
onCreateOptionsMenu | 进入Launcher后第一次点MENU按钮时触发 | 1、切换语言,android在选择不同的语言之后会调用IActivityManager的实例updateConfiguration。2、kill掉ADW的Launcher也行。 |
onPrepareOptionsMenu | 有两种情况:1、进入Launcher后第一次点MENU按钮触发onCreateOptionsMenu后调用;2、经过第一步,以后每次点MENU按钮时都会触发 | 点击MENU触发 |
onOptionsItemSelected | 选择MENU里面的任意内容后触发 | 点击NEMU里的任意选项 |
三段代码如下:
onCreateOptionsMenu(Menu
menu)
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- if (mDesktopLocked && mSavedInstanceState == null) return false;
- super.onCreateOptionsMenu(menu);
- menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
- .setIcon(android.R.drawable.ic_menu_add)
- .setAlphabeticShortcut(‘A‘);
- menu.add(MENU_GROUP_NORMAL, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
- .setIcon(android.R.drawable.ic_menu_gallery)
- .setAlphabeticShortcut(‘W‘);
- menu.add(MENU_GROUP_NORMAL, MENU_SEARCH, 0, R.string.menu_search)
- .setIcon(android.R.drawable.ic_search_category_default)
- .setAlphabeticShortcut(SearchManager.MENU_KEY);
- menu.add(MENU_GROUP_NORMAL, MENU_NOTIFICATIONS, 0, R.string.menu_edit)
- .setIcon(android.R.drawable.ic_menu_edit)
- .setAlphabeticShortcut(‘E‘);
- final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
- settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
- Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
- menu.add(MENU_GROUP_NORMAL, MENU_SETTINGS, 0, R.string.menu_settings)
- .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut(‘P‘)
- .setIntent(settings);
- //ADW: add custom settings
- menu.add(MENU_GROUP_NORMAL, MENU_ALMOSTNEXUS, 0, R.string.menu_adw_settings)
- .setIcon(android.R.drawable.ic_menu_preferences)
- .setAlphabeticShortcut(‘X‘);
- menu.add(MENU_GROUP_CATALOGUE, MENU_APP_GRP_CONFIG, 0, R.string.AppGroupConfig)
- .setIcon(android.R.drawable.ic_menu_agenda);
- //menu.add(MENU_GROUP_CATALOGUE, MENU_APP_GRP_RENAME, 0, R.string.AppGroupRename)
- // .setIcon(R.drawable.ic_menu_notifications);
- menu.add(MENU_GROUP_CATALOGUE, MENU_APP_SWITCH_GRP, 0, R.string.AppGroupChoose)
- .setIcon(android.R.drawable.ic_menu_manage);
- menu.add(MENU_GROUP_CATALOGUE, MENU_APP_DELETE_GRP, 0, R.string.AppGroupDel)
- .setIcon(android.R.drawable.ic_menu_delete);
- return true;
- }
onPrepareOptionsMenu(Menu menu)
- @Override
- public boolean onPrepareOptionsMenu(Menu menu) {
- super.onPrepareOptionsMenu(menu);
- if(mIsEditMode || mIsWidgetEditMode)return false;
- // We can‘t trust the view state here since views we may not be done binding.
- // Get the vacancy state from the model instead.
- mMenuAddInfo = mWorkspace.findAllVacantCellsFromModel();
- menu.setGroupVisible(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid && (!allAppsOpen) );
- menu.setGroupVisible(MENU_GROUP_NORMAL, !allAppsOpen);
- menu.setGroupVisible(MENU_GROUP_CATALOGUE, allAppsOpen);
- return true;
- }
onOptionsItemSelected(MenuItem item)
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- switch (item.getItemId()) {
- case MENU_ADD:
- addItems();
- return true;
- case MENU_WALLPAPER_SETTINGS:
- startWallpaper();
- return true;
- case MENU_SEARCH:
- onSearchRequested();
- return true;
- case MENU_NOTIFICATIONS:
- //showNotifications();
- //ADW: temp usage for desktop eiting
- if(allAppsOpen)closeAllApps(false);
- startDesktopEdit();
- return true;
- case MENU_ALMOSTNEXUS:
- showCustomConfig();
- return true;
- case MENU_APP_GRP_CONFIG:
- showAppList();
- return true;
- case MENU_APP_GRP_RENAME:
- showNewGrpDialog();
- return true;
- case MENU_APP_SWITCH_GRP:
- showSwitchGrp();
- return true;
- case MENU_APP_DELETE_GRP:
- showDeleteGrpDialog();
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
解释下一些方法,便于记忆:
代码中用到的方法用绿色标注,官方解释为红色,我的理解为蓝色。
menu.add(MENU_GROUP_ADD,
MENU_ADD, 0,
R.string.menu_add)
.setIcon(android.R.drawable.ic_menu_add)
.setAlphabeticShortcut(‘A‘);
MenuItem
android.view.Menu.add(int groupId, int itemId, int order, int
titleRes)
Variation on add(int, int, int, CharSequence) that takes a
string resource identifier instead of the string
itself.
参数:
groupId The group identifier that this item
should be part of. This can also be used to define groups of items for batch
state changes. Normally use NONE if an item should not be in a
group.
itemId Unique item ID. Use NONE if you do not need a unique
ID.
order The order for the item. Use NONE if you do not care about the
order. See MenuItem.getOrder().
titleRes Resource identifier of title
string.
返回:
The newly added menu
item.
groupId:分组ID
itemId:各项自己的UID
order:我理解为目录,还没具体看
titleRes:显示文本信息
setIcon方法:设置文字上方的图标
setAlphabeticShortcut方法:在MENU菜单下的快捷键,不是全键盘的机子几乎不会用到的。
menu.setGroupVisible(MENU_GROUP_ADD,
mMenuAddInfo != null && mMenuAddInfo.valid && (!allAppsOpen)
);
void android.view.Menu.setGroupVisible(int
group, boolean visible)
Show or hide all menu items that are in the
given group.
参数:
group The group of items to operate
on.
visible If true the items are visible, else they are
hidden.
另请参阅:
MenuItem.setVisible
设置各个groupId组是够显示的方法
group:其实就是android.view.Menu.add里面的groupId
visible:是否显示该组
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。