1. Normal Menu
Overwrite the Oncreateoptionsmenu (Menu menu) method in activity, which is responsible for the production of menu, which is a callback function that the Android system will produce when it presses the Menubutton on the mobile device.
public boolean oncreateoptionsmenu (Menu menu) {
//menu.add (arg0, Arg1, arg2, Arg3) Add a menu to the button
//the first parameter: The ID of the group, assuming that no grouping is set to 0
//The second parameter: the ID of each item in the menu, this ID should be unique
//The third parameter: sort the information, depending on the size of the field, menu to sort the items in: Small row in front
//Fourth: Menu item display information
menu.add (0, Menu_item_1, 0, "APPLE");
menu.add (0, menu_item_2, 0, "ORANGE");
return super.oncreateoptionsmenu (menu);
}
If you want to join the Click event, overwrite the onoptionsitemselected (MenuItem Item) method.
public boolean onoptionsitemselected (MenuItem item) {
//the item passed by ID inference, This ID is given by Item.getitemid () to
switch (Item.getitemid ()) {
case menu_item_1:
t = Toast.maketext (This, "you choose the Apple", Toast.length_short);
t.show ();
break;
case menu_item_2:
t = Toast.maketext (This, "you choose the Orange", toast.length_ short);
t.show ();
break;
}
return true;
}
2. submenu (submenu)
public boolean oncreateoptionsmenu (Menu menu) {
menu.add (0, Menu_item_1, 1," APPLE ");
menu.add (0, Menu_item_2, 2, "ORANGE");
// Create a submenu object for menu
submenu submenu = Menu.addsubmenu (0, Menu_item_3, 3, "BANANA");
// Add an option to submenu
submenu.add (0,,, "BIG BANANA");
submenu.add (0, +, +, "SMALL BANANA");
return super.oncreateoptionsmenu (menu);
}
3. Context menu (ContextMenu)--a menu that pops up after a specific view is long pressed
No matter what view you can register ContextMenu, the most common is the item used for the ListView in the list view.
ContextMenu for the View register in the OnCreate method.
protected void OnCreate (Bundle savedinstancestate) {
super.oncreate ( Savedinstancestate);
setcontentview (R.layout.contextmenu);
btn1 = (Button) Findviewbyid (R.ID.BTN1);
btn2 = (Button) Findviewbyid (R.ID.BTN2);
//is a button ContextMenu
registerforcontextmenu (BTN1);
registerforcontextmenu (BTN2);
}
Override the activity's Oncreatecontenxtmenu () method, and Call menu's Add method to add an item (MenuItem)
public void Oncreatecontextmenu (ContextMenu menu, View V,
Contextmenuinfo menuinfo) {
if (V==BTN1) {
Set Menu title
Menu.setheadertitle ("This is the Frist");
Add to Menu
Menu.add (0, 0, "context menu 1");
Menu.add (0, 201, 0, "context menu 2");
}
if (V==BTN2) {
Set Menu title
Menu.setheadertitle ("This is the second");
Add to Menu
Menu.add (0, 0, "context menu 3");
Menu.add (0, 301, 0, "context menu 4");
}
Super.oncreatecontextmenu (menu, V, menuinfo);
}
4. Dynamic Menu
Onprepareoptionsmenu () determines which menus show which ones are not displayed when the menu list is about to be displayed, and of course can do some other work.
public boolean Onprepareoptionsmenu (Menu menu) {
String Currenttext = Textview.gettext (). toString ();
if ("M". Equals (Currenttext)) {
Clear out the menu first
Menu.clear ();
You can change the value of the TextView by clicking on the menu item, so (n) you can test it.
MenuItem item = menu.add (0, +, 401, "to N");
Android comes with the icon
Item.seticon (Android. R.drawable.alert_dark_frame);
}
if ("N". Equals (Currenttext)) {
Clear out the menu first
Menu.clear ();
You can change the value of the TextView by clicking on the menu item so that you can test it (into m).
MenuItem item = menu.add (0, 401, 402, "to M");
Item.seticon (Android. R.drawable.alert_light_frame);
}
Now together with two menu subkeys
Menu.add (0, 402, 403, "now is" + currenttext);
return Super.onprepareoptionsmenu (menu);
}
5. Create a menu in XML file mode
Create a folder under the Res/folder, named menu, with an XML file named Menu_xml_file.xml
<?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" &NBSP;
android:title= "This 2"/>&NBSP;
</ Group>
</menu>
Overriding the Oncreateoptionsmenu (Menu menu) method in activity
public boolean Oncreateoptionsmenu (Menu menu) {
The Menuinflater class is used to instantiate a menu XML file into a menu object
Get current Menu
Menuinflater inflater = Getmenuinflater ();
Inflate (int menures, menu menu) Fill Menu
Populate the menu from a specified XML resource, assuming that an error throws Inflateexception
Menures the resource ID to load into the XML layout file
menu to populate, these items and submenus are added to the menu you want to populate
Inflater.inflate (r.menu.menu_xml_file, menu);
return true;
}
The other is the same as creating a menu in activity.
Tip: When you click Menubutton, there are probably two functions that are recalled in activity:
The first is onoptionsitemselected (), which is only used for the first click of Menubutton in the current app, and will not be recalled at a later time.
The second is Onprepareoptionsmenu (), which is called by the system when the menu is displayed after each click of the Menubutton, and this function is recalled every time the menu is displayed. We generally change the logic of the menu display according to the conditions placed in this function.
Transferred from: http://zhouyuting714.blog.163.com/blog/static/1811643182011327101931489/
Android Learning Menu