android中菜单的使用

 

	 @Override
	    public boolean onCreateOptionsMenu(Menu menu) {
	        super.onCreateOptionsMenu(menu);
	        //all books menu
	        menu.add(MENU_GROUP_BOOKS, COPY_FORE_CARD, 0, R.string.lib_menu_imput).setIcon(R.drawable.icon_copy_from_card);
	        menu.add(MENU_GROUP_BOOKS, ESTORE,0, R.string.lib_menu_store).setIcon( R.drawable.icon_shop);
	        menu.add(MENU_GROUP_BOOKS, SETTINGS, 0, R.string.lib_menu_set).setIcon(R.drawable.icon_setting);
	        
	        //more menu
	        menu.add(MENU_GROUP_MORE, ADD_CUSTOM_CATEGORY, 0, R.string.lib_menu_add_category).setIcon(R.drawable.icon_add);
//	        menu.add(MENU_GROUP_MORE, COPY_FORE_CARD, 0, R.string.lib_menu_imput).setIcon(R.drawable.icon_copy_from_card);
	        menu.add(MENU_GROUP_MORE, ESTORE,0, R.string.lib_menu_store).setIcon( R.drawable.icon_shop);
	        return true;
	    }
	 


    @Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		if (isSearch) {
			menu.setGroupVisible(MENU_GROUP_BOOKS, false);
			menu.setGroupVisible(MENU_GROUP_MORE, false);
		} else if (TAB_MORE.equals(currentTab)) {
			menu.setGroupVisible(MENU_GROUP_BOOKS, false);
			menu.setGroupVisible(MENU_GROUP_MORE, true);
		} else if(TAB_ALL.equals(currentTab)) {
			menu.setGroupVisible(MENU_GROUP_BOOKS, true);
			menu.setGroupVisible(MENU_GROUP_MORE, false);
		} else {
			menu.setGroupVisible(MENU_GROUP_BOOKS, true);
			menu.setGroupVisible(MENU_GROUP_MORE, false);
			menu.findItem(COPY_FORE_CARD).setVisible(false);
		}
		return super.onPrepareOptionsMenu(menu);
	}

  如上代码是覆盖activity后的代码,

  对于onPrepareOptionMenu,sdk上有如下说明

Prepare the Screen's standard options menu to be displayed. This is called right before the menu is shown, every time it is shown. You can use this method to efficiently enable/disable items or otherwise dynamically modify the contents.

The default implementation updates the system menu items based on the activity's state. Deriving classes should always call through to the base class implementation.

可以在onCreateOptionsMenu()方法里面先声明对应的菜单项目栏,然后在onPrepareOptionMenu方法中声明飞是否显示

 

你可能感兴趣的:(android)