1. The normal menu
Let's see how the simplest menu is implemented.
Overrides the Oncreateoptionsmenu (Menu menu) method in the main activity.
Code@Override
public boolean Oncreateoptionsmenu (Menu menu) {
TODO auto-generated Method Stub
Menu.add (0, 1, 1, "Apple");
Menu.add (0, 2, 2, "banana");
return Super.oncreateoptionsmenu (menu);
}
This will have two menu options. If you want to add a click event, overwrite the onoptionsitemselected (MenuItem Item) method.
Code@Override
public boolean onoptionsitemselected (MenuItem item) {
TODO auto-generated Method Stub
if (item.getitemid () = = 1) {
Toast T = toast.maketext (This, "You chose Apple", Toast.length_short);
T.show ();
}
else if (item.getitemid () = = 2) {
Toast T = toast.maketext (This, "You have chosen Bananas", toast.length_short);
T.show ();
}
return true;
}
2. Submenu
The production of submenu is also simple, add a few sentences to the first code oncreateoptionsmenu (Menu menu) method, as follows:
Code@Override
public boolean Oncreateoptionsmenu (Menu menu) {
TODO auto-generated Method Stub
Menu.add (0, 1, 1, "Apple");
Menu.add (0, 2, 2, "banana");
Submenu submenu = Menu.addsubmenu (1, 100, 100, "peach");
Submenu.add (2, 101, 101, "Big Peach");
Submenu.add (2, 102, 102, "Little Peach");
return true;
}
Click "Peach" will appear submenu, there are two sub-options, respectively, "Big peach" and "small peach."
3. Context Menu
Similar to the right mouse button on your computer, press and hold the menu that pop up after a view.
First, define a number of buttons in the Main.xml. Then overwrite the Oncreatecontextmenu (ContextMenu menu, View V, Contextmenuinfo menuinfo) method. The code inside is as follows:
First, to register, in the OnCreate method, the following code:
Code@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
B1 = (Button) Findviewbyid (R.ID.B1);
B2 = (Button) Findviewbyid (R.ID.B2);
Registerforcontextmenu (B1);
Registerforcontextmenu (B2);
}
Code@Override
public void Oncreatecontextmenu (ContextMenu menu, View V,
Contextmenuinfo menuinfo) {
TODO auto-generated Method Stub
if (V==B1) {
Menu.setheadertitle ("This is 1");
Menu.add (A. 1, "Context menu,");
Menu.add (201, 201, "Context menu 2");
}
else if (V==B2) {
Menu.setheadertitle ("This is 2");
Menu.add (b, 1);
Menu.add (301, 301, "C 2");
}
Super.oncreatecontextmenu (menu, V, menuinfo);
}
4. Dynamic Menu
Dynamic menus have different menus depending on the interface. The following code implements this function: when the value of a TextView in the main interface is "M" and "N", a different menu pops up.
Code@Override
public boolean Onprepareoptionsmenu (Menu menu) {
TODO auto-generated Method Stub
String Currenttext = Tv1.gettext (). toString ();
if ("M". Equals (Currenttext)) {
Menu.clear ();//Clear out the menu first
MenuItem item = menu.add (0, +, 401, "to N");//You can change the value of the TV1 by clicking on the menu item (n) to test the
Item.seticon (Android. R.drawable.alert_dark_frame);//android's own icon
}
if ("N". Equals (Currenttext)) {
Menu.clear ();//Clear out the menu first
MenuItem item = menu.add (0, 401, 402, "to M");//You can change the value of the TV1 by clicking on the menu item so that you can test it (into m).
Item.seticon (Android. R.drawable.alert_light_frame);
}
Menu.add (0, 402, 403, "now is" + currenttext);//There are two menu subkeys
return Super.onprepareoptionsmenu (menu);
}
5. Create a menu in XML file mode
Previously, the menu was created in code, and it was easy to make menus with an XML configuration file.
To res/directory to build a folder, named menu, the following an XML file, named Menu_xml_file.xml, the code is as follows:
Code<?xml version= "1.0" encoding= "Utf-8"?>
<menu xmlns:android= "Http://schemas.android.com/apk/res/android" >
<group android:id= "@+id/grout_main" >
<item android:id= "@+id/menu_1"
Android:title= "This 1"/>
<item android:id= "@+id/menu_2"
Android:title= "This 2"/>
</group>
</menu>
In the activity, overwrite the Oncreateoptionsmenu (Menu menu) method with the following code:
Code@Override
public boolean Oncreateoptionsmenu (Menu menu) {
TODO auto-generated Method Stub
Menuinflater inflater = Getmenuinflater ();
Inflater.inflate (r.menu.menu_xml_file, menu);
return true;
}
The rest is the same as creating a menu in activity.