The menu includes the Options menu (Optionmenu), the context menu (ContextMenu), and the submenu (submenu).
Steps to create a submenu (submenu):
1, rewrite the activity of the Oncreateoptionmenu (Menu menu) method, in this method through the Menu object method to add menus.
2. Response menu click event, overriding Activity onoptionitemselected (MenuItem mi) method
The code is as follows:
public class Mainactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Create submenu submenu Submenu1=menu.addsubmenu ("submenu 1");// Set the header of the menu header Submenu1.setheadertitle ("Heading 1 of the menu header");//Set menu icon Submenu1.seticon (r.drawable.ic_launcher);// Set the icon for the menu header Submenu1.setheadericon (r.drawable.ic_launcher),//Add the menu item to the 0 group Submenu1.add (0, 0, 0, "menu item 1"), Submenu1.add (0, 1, 0, "menu item 2"); Submenu1.add (0, 2, 0, "menu item 3"); return true; @Overridepublic boolean onoptionsitemselected (MenuItem Item) {//TODO auto-generated method Stubtoast (Item.gettitle ()) ; return super.onoptionsitemselected (item);} private void Toast (charsequence title) {//TODO auto-generated method Stubtoast.maketext (Mainactivity.this,title, Toast.length_short). Show ();}}
When you click menu:
Click Sub-Menu 1:
Learn about Android Ax (6)