The menu is a common user interface element, and there are 3 types of menus available in the Android system.
(1) Options Menu: The menu displayed on the Device by menu key.
(2) context menu: Long press a specific interface view display, with the specific view bound together, similar to the right mouse button on the PC menu.
(3) submenu (Sub menu): Both of the above menus can be added to a submenu, but a submenu cannot be nested in a sub-menu.
Let's use an example to illustrate the basic usage of 3 kinds of menus.
The XML resource file for the menu is placed under the Res/menu directory.
Optionmenu.xml
1. <?xml version= "1.0" encoding= "Utf-8"?>
2. <menu xmlns:android= "Http://schemas.android.com/apk/res/android" >
3. <item android:id= "@+id/optionitem1"
4. android:icon= "@drawable/icon"
5. android:title= "option 1"/>
6. <item android:id= "@+id/optionitem2"
7. android:icon= "@drawable/ic_launcher"
8. android:title= "Option 2"/>
9. <item android:id= "@+id/optionitem3"
android:icon= "@drawable/icon"
android:title= "submenu" >
<menu>
<item android:id= "@+id/subitem1"
Android:title= "Sub-menu item 1"/>
<item android:id= "@+id/subitem2"
Android:title= "@string/Sub-menu item 2"/>
</menu>
</item>
</menu>
In this menu resource file, 3 menu items are defined, with IDs optionitem1, OPTIONITEM2, and Optionitem3 respectively. Each menu item item has its own ID, title, and icon.
The Optionitem3 menu item in line 9th to 18th adds the sub-menu structure shown in line 12th to 17th, in addition to the ID, title, and icon. The submenu item has only ID and title and cannot set icon.
The context menu resource file.
Contextmenu.xml
1. <?xml version= "1.0" encoding= "Utf-8"?>
2. <menu xmlns:android= "Http://schemas.android.com/apk/res/android" >
3. <item
4. android:id= "@+id/contextitem1"
5. Android:title= "context menu subkey 1" >
6. </item>
7. <item
8. android:id= "@+id/contextitem2"
9. Android:title= "context menu subkey 2" >
Ten. </item>
<item.
Android:id= "@+id/contextitem3"
Android:title= "context menu subkey 3" >
</item>
</menu>.
The context menu is formatted like a submenu, and the item item does not have an icon property.
The Java source code for the menu example is as follows.
Mymenu.java
1. @Override
2. public void OnCreate (Bundle savedinstancestate) {
3. Super.oncreate (savedinstancestate);
4. Setcontentview (R.layout.main);
5. GridView GridView;
6. string[] titles={"Zhao 1", "Qian 2", "Zhang San", "John Doe", "Harry"};
7. int buf=r.drawable.sample;
8. int[] Resids={buf,buf,buf,buf,buf};
9. gridview= (GridView) This.findviewbyid (R.id.gridview);
Gridview.setadapter (New Myadapter (Titles,resids));
Registerforcontextmenu (GridView);
Gridview.setonitemclicklistener (New Adapterview.onitemclicklistener () {
13 ...});
@Override
. public void Oncreatecontextmenu (ContextMenu menu,view
V,contextmenu.contextmenuinfo menuinfo) {
Super.oncreatecontextmenu (Menu,v,menuinfo);
Getmenuinflater (). Inflate (R.menu.contextmenu,menu);
18.}
@Override
public boolean oncontextitemselected (MenuItem item) {
super.oncontextitemselected (item);
Switch (Item.getitemid ()) {
Case R.ID.CONTEXTITEM1:
Toast.maketext (This, "context menu subkey 1", Toast.length_short). Show ();
break;
Case R.ID.CONTEXTITEM2:
Toast.maketext (This, "context menu subkey 2", Toast.length_short). Show ();
break;
Case R.ID.CONTEXTITEM3:
Toast.maketext (This, "context menu subkey 3", Toast.length_short). Show ();
break;
Default:
break;
34.}
return super.onoptionsitemselected (item);
36.}
Panax Notoginseng. @Override
. public boolean Oncreateoptionsmenu (Menu menu) {
Super.oncreateoptionsmenu (menu);
Getmenuinflater (). Inflate (R.menu.optionmenu, menu);
A. return true;
42.}
@Override.
. public boolean onoptionsitemselected (MenuItem Item)
45. {
Switch (Item.getitemid ())
47. {
Case R.ID.OPTIONITEM1:
Toast.maketext (This, "click Option 1", Toast.length_short). Show ();
. return true;
Case R.ID.OPTIONITEM2:
Toast.maketext (This, "click Option 2", Toast.length_short). Show ();
. return true;
Case R.ID.OPTIONITEM3:
Toast.maketext (This, "click Option 3", Toast.length_short). Show ();
. return true;
Case R.ID.SUBITEM1:
Toast.maketext (This, click Sub-menu option 1, Toast.length_short). Show ();
. return true;
Case R.ID.SUBITEM2:
Toast.maketext (This, "Click submenu option 2", Toast.length_short). Show ();
. return true;
. Default:
. return super.onoptionsitemselected (item);
65.}}
Myadapter public class extends Baseadapter {
67 ...}
In line 11th Registerforcontextmenu (GridView), we register a context menu for the GridView, and a context menu pops up when the Itemview in the GridView receives a long press event.
Line 14th to 18th we rewrite Oncreatecontextmenu, the callback method that creates the context menu. This method is called every time the context menu triggers the display.
Line 17th We use the Getmenuinflater (). Inflate method to load the menu resource XML file ContextMenu into the menu object, which is the context menu object passed in by the Oncreatecontextmenu callback function.
The Oncontextitemselected method in line 19th to 36th is the system callback method when the user selects an option in the context menu, and the system passes in the MenuItem object selected by the user. by Item.getitemid () We know the ID number of the menu item selected by the user. Here we use Toast to display the user's selection information.
Line 37th to 42nd Oncreateoptionsmenu is the callback method that creates the Options menu, which is called the first time the option menu is created. We have rewritten this method to load the menu resource Optionmenu into the Options menu.
Line 43rd to 65th is the item response callback function for the Options menu, and we also get the option ID number by Item.getitemid ().
Run 10-14, figure 10-15, figure 10-16, and figure 10-17.
▲ Figure 10-14 Options menu diagram ▲ Figure 10-15 Click on the icon after option 2
Figure 10-16 Sub-menu diagram ▲ Figure 10-17 context menu diagram
Android Menu-menu