Android menu is divided into three categories, Options menu (Optionmenu), context menu (ContextMenu), submenu (submenu)
Options Menu
There are two ways to add a menu of options, one in Menu.xml, which is described in the Android resource Details (ii) menu resource, and the second is added in. java
1, covering the activity of the Oncreateoptionsmenu (Menu menu) method, when we first open the menu
The method is called automatically.
2. The Add () method of the menu (Menultem) is called, and you can call Menultem's SetIcon ()
method to set the icon for a menu item: 1
3. When the menu item (Menuitem) is selected, cover the activity's onoptionsitemselected () method to respond to the event.
The following code:
public class mainactivity extends activity { private static final int menu1=Menu.FIRST; private static final int menu2=menu.first+1; @Override protected void oncreate (bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); } public boolean oncreateoptionsmenu (menu menu) { menu.add (0, menu1,0, "OK"); menu.add (0,menu2,0, "Cancel"); return true; }}
context Menu
When the user presses the screen, the popup menu becomes the context menu.
1, covering the activity of the Oncreatecontextmenuo method, call the menu's Add method to add a menu item
2. Override the Oncontextitemselected (Menuiten Item) method to respond to menu click events.
3. Call the Registerforcontextmenu () method to register the context menu for the view. When you long press the view, the context menu appears. The code is as follows:
Public class mainactivity extends activity {private static final int menu1=Menu.FIRST;private static final int menu2=Menu.FIRST+1; @Override protected void oncreate (bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); textview mytext= ( TextView) This.findviewbyid (R.ID.TEXT1); This.registerforcontextmenu (MyText); } public void Oncreatecontextmenu (Contextmenu menu,view v,contextmenuinfo menuinfo) { menu.add (0,menu1,0, "OK"); Menu.add (0,menu2,0, "Cancel"); } public boolean oncontextitemselected (Menuitem item) { int id=item.getgroupid (); switch (ID) { case menu1: break; case menu2:break; } return super.oncontextitemselected (item); }}
Sub-Menu
A submenu is a menu of multi-level display of groupings of the same functionality, such as new, open, close, and so on in the Windows File menu. There are also a lot of submenus in the Android system, such as the "Settings" menu, which has its sub-menu.
The steps are as follows:
1, covering the activity of the Oncreateoptionsmenu () method, call the Menu of the Addsubmenu () method to add
Add Sub-menu item (submenu).
2. Call the Add () method of submenu, adding a submenu (submenu).
3. Override the Oncontextitemselected () method to respond to menu click events.
The code is as follows:
public class mainactivity extends activity { private static final int menu1=menu.first; private static final int menu2=menu.first+1; @Override protected void OnCreate (bundle savedinstancestate) { super.oncreate ( Savedinstancestate); setcontentview (R.layout.activity_main); } public boolean oncreateoptionsmenu (Menu menu) { submenu file=menu.addsubmenu ("file");//Add submenu submenu edit=menu.addsubmenu ("edit");//Add submenu file.add (0,menu1,0, "new");//Add menu options for submenus file.add (0,menu2,0, "open"); return true; }}
This article is from the "Useless Uncle" blog, please be sure to keep this source http://aslonely.blog.51cto.com/6552465/1614885
Android Menu Explanation