Android开发笔记: Menu

Menu的文章很多,这里只是自己开发时做的实验而已.

 

Menu Types: 有Options menus, Context menus , Sub menus 三种菜单类型.

 

Android different menu types support different features:

  1. Context menus : Do not support item shortcuts and item icons.
  2. Options menus : The  icon menus   do not support item check marks and only show the item's  condensed title . The  expanded menus   (only available if six or more menu items are visible, reached via the 'More' item in the icon menu) do not show item icons, and item check marks are discouraged.
  3. Sub menus : Do not support item icons, or nested sub menus.
Menu Create: 可以通过从./res/menu/xxx.xml中添加,还有就是通过代码直接动态添加。  

    

    从资源中添加比较直观,以后修改也会方便一些,但不太灵活;通过资源添加的话就不多说了,想做着都行。两种方法各有优缺点,得根据项目情况而定。

 

On Menu Create Codes:

    Options menus :

// Override onCreateOptionsMenu //This is only called once, the first time the options menu is displayed. //To update the menu every time it is displayed, see onPrepareOptionsMenu(Menu). @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); // 通过从./res/menu/xxx.xml中添加 if (bIsCreateFromResource) { getMenuInflater().inflate(R.menu.main_menu, menu); Toast.makeText(this, "onCreateOptionsMenu by Resource", Toast.LENGTH_SHORT).show(); } // 通过代码动态添加 else { /*public abstract MenuItem add (int groupId, int itemId, int order, CharSequence title) Add a new item to the menu. This item displays the given title for its label. Parameters 1. groupId The group identifier that this item should be part of. This can be used to define groups of items for batch state changes. Normally use NONE if an item should not be in a group. 2. itemId Unique item ID. Use NONE if you do not need a unique ID. 3. order The order for the item. Use NONE if you do not care about the order. See getOrder(). 4. title The text to display for the item. */ menu.add(Menu.NONE, R.id.menu_add, 1, "添加").setIcon( android.R.drawable.ic_menu_add).setShortcut('1', 'a'); menu.add(Menu.NONE, R.id.menu_save, 2, "保存").setIcon( android.R.drawable.ic_menu_save); menu.add(Menu.NONE, R.id.menu_send, 3, "发送").setIcon( android.R.drawable.ic_menu_send); menu.add(Menu.NONE, R.id.menu_info_details, 4, "详细").setIcon( android.R.drawable.ic_menu_info_details); menu.add(Menu.NONE, R.id.menu_delete, 5, "删除").setIcon( android.R.drawable.ic_menu_delete); // Expand Menu Item // Expand Menu Item的setIcon将无效 menu.add(Menu.NONE, R.id.menu_help, 6, "帮助").setIcon( android.R.drawable.ic_menu_help); menu.add(Menu.NONE, R.id.menu_expand_item1, 7, "Expand Menu Item 1"); menu.add(Menu.NONE, R.id.menu_expand_item2, 8, "Expand Menu Item 2"); menu.add(Menu.NONE, R.id.menu_expand_item3, 9, "Expand Menu Item 3"); Toast.makeText(this, "onCreateOptionsMenu by Code", Toast.LENGTH_SHORT).show(); } return true; } //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. @Override public boolean onPrepareOptionsMenu(Menu menu){ Toast.makeText(this, "onPrepareOptionsMenu", Toast.LENGTH_SHORT).show(); return true; } //This hook is called whenever the options menu is being closed //(either by the user canceling the menu with the back/menu button, or when an item is selected). @Override public void onOptionsMenuClosed(Menu menu){ Toast.makeText(this, "onOptionsMenuClosed", Toast.LENGTH_SHORT).show(); } // Override onOptionsItemSelected //Returns //boolean Return false to allow normal menu processing to proceed, true to consume it here. @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_add: Toast.makeText(this, "点击/"添加/"菜单项", Toast.LENGTH_SHORT).show(); break; case R.id.menu_save: Toast.makeText(this, "点击/"保存/"菜单项", Toast.LENGTH_SHORT).show(); break; case R.id.menu_send: Toast.makeText(this, "点击/"发送/"菜单项", Toast.LENGTH_SHORT).show(); break; case R.id.menu_info_details: Toast.makeText(this, "点击/"详细/"菜单项", Toast.LENGTH_SHORT).show(); break; case R.id.menu_delete: Toast.makeText(this, "点击/"删除/"菜单项", Toast.LENGTH_SHORT).show(); break; case R.id.menu_help: Toast.makeText(this, "Expand Menu Item /"帮助/"", Toast.LENGTH_SHORT).show(); break; case R.id.menu_expand_item1: Toast.makeText(this, "Expand Menu Item 1", Toast.LENGTH_SHORT).show(); break; case R.id.menu_expand_item2: Toast.makeText(this, "Expand Menu Item 2", Toast.LENGTH_SHORT).show(); break; case R.id.menu_expand_item3: Toast.makeText(this, "Expand Menu Item 3", Toast.LENGTH_SHORT).show(); break; default: break; } return false; }

 

       Context menus & Sub menus:


private static final int MENU_GROUP_ONE = 1; private static final int MENUITME_CHECKBOX = Menu.FIRST + 1; private static final int MENUITME_RADIOBOX_RED = Menu.FIRST + 2; private static final int MENUITME_RADIOBOX_GREEN = Menu.FIRST + 3; private static final int MENUITME_RADIOBOX_BLUE = Menu.FIRST + 4; // Override onCreateContextMenu //Called when a context menu for the view is about to be shown. //Unlike onCreateOptionsMenu(Menu), this will be called every time the context menu //is about to be shown and should be populated for the view (or item inside the view for //AdapterView subclasses, this can be found in the menuInfo)). @Override public void onCreateContextMenu (ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); if (bIsCreateFromResource) { getMenuInflater().inflate(R.menu.context_menu, menu); // 没有找到方法在XML里面设置,所以得用代码来实现 menu.setHeaderIcon(R.drawable.icon); menu.setHeaderTitle("Context Menu"); SubMenu subMenu = menu.findItem(R.id.menu_submenu).getSubMenu(); subMenu.setHeaderIcon(R.drawable.icon); subMenu.setHeaderTitle("Sub Menu Item"); subMenu.setGroupCheckable(R.id.menu_group_one, true, true); Toast.makeText(this, "onCreateContextMenu by Resource", Toast.LENGTH_SHORT).show(); } else { menu.setHeaderTitle("Context Menu"); menu.setHeaderIcon(R.drawable.icon); /*...........................begin sub menu...........................*/ SubMenu subMenu = menu.addSubMenu("Sub Menu Item"); subMenu.setHeaderIcon(R.drawable.icon); subMenu.add(Menu.NONE, MENUITME_CHECKBOX, Menu.NONE, "CheckBox"). setCheckable(true).setChecked(true); subMenu.add(MENU_GROUP_ONE, MENUITME_RADIOBOX_RED, Menu.NONE, "Red"). setCheckable(true).setChecked(false); subMenu.add(MENU_GROUP_ONE, MENUITME_RADIOBOX_GREEN, Menu.NONE, "Green"). setCheckable(true).setChecked(true); subMenu.add(MENU_GROUP_ONE, MENUITME_RADIOBOX_BLUE, Menu.NONE, "Bule"). setCheckable(true).setChecked(false); subMenu.setGroupCheckable(MENU_GROUP_ONE, true, true); /*...........................end sub menu...........................*/ menu.add(Menu.NONE, R.id.menu_add, Menu.NONE, "添加"); menu.add(Menu.NONE, R.id.menu_save, Menu.NONE, "保存"); menu.add(Menu.NONE, R.id.menu_send, Menu.NONE, "发送"); menu.add(Menu.NONE, R.id.menu_info_details, Menu.NONE, "详细"); menu.add(Menu.NONE, R.id.menu_delete, Menu.NONE, "删除"); menu.add(Menu.NONE, R.id.menu_help, Menu.NONE, "帮助"); Toast.makeText(this, "onCreateContextMenu by Code", Toast.LENGTH_SHORT).show(); } } // Override onContextMenuClosed //This hook is called whenever the context menu is being closed //(either by the user canceling the menu with the back/menu button, or when an item is selected). public void onContextMenuClosed (Menu menu) { Toast.makeText(this, "onContextMenuClosed", Toast.LENGTH_SHORT).show(); } // Override onContextItemSelected //boolean Return false to allow normal context menu processing to proceed, true to consume it here. @Override public boolean onContextItemSelected (MenuItem item) { super.onContextItemSelected(item); switch (item.getItemId()) { case R.id.menu_add: Toast.makeText(this, "点击/"添加/"菜单项", Toast.LENGTH_SHORT).show(); break; case R.id.menu_save: Toast.makeText(this, "点击/"保存/"菜单项", Toast.LENGTH_SHORT).show(); break; case R.id.menu_send: Toast.makeText(this, "点击/"发送/"菜单项", Toast.LENGTH_SHORT).show(); break; case R.id.menu_info_details: Toast.makeText(this, "点击/"详细/"菜单项", Toast.LENGTH_SHORT).show(); break; case R.id.menu_delete: Toast.makeText(this, "点击/"删除/"菜单项", Toast.LENGTH_SHORT).show(); break; case R.id.menu_help: Toast.makeText(this, "点击/"帮助/"菜单项", Toast.LENGTH_SHORT).show(); break; case MENUITME_CHECKBOX: Toast.makeText(this, "CheckBox Selected", Toast.LENGTH_SHORT).show(); break; case MENUITME_RADIOBOX_RED: Toast.makeText(this, "RedioBox Red Selected", Toast.LENGTH_SHORT).show(); break; case MENUITME_RADIOBOX_GREEN: Toast.makeText(this, "RedioBox Green Selected", Toast.LENGTH_SHORT).show(); break; case MENUITME_RADIOBOX_BLUE: Toast.makeText(this, "RedioBox Blue Selected", Toast.LENGTH_SHORT).show(); break; default: break; } return false; }

 

 

你可能感兴趣的:(android,delete,processing,button,menu,icons)