Three ways to create a menu
One, Optionsmenu---options menu
Menus in Android apps are hidden by default, and the menu is displayed only when the user taps the menu key on the phone. This menu is called the Options menu or the System menu. Starting from 3.0, Android does not require a menu button on the mobile device. So Android recommends using Actionbar instead of menu. menu display differences in different versions
Second, ContextMenu---context menu
Three, PopupMenu---pop-up menu
You can set a pop-up menu on a specified component, by default, the pop-up menu appears below or above the component, PopupMenu can add multiple menu items, or you can add a submenu item to a menu item.
To create a step:
1. Call New PopupMenu (Mainactivity.this,view view), view represents the component that fires the popup menu.
2. Call the Menuinflater () method to populate the menu with the PopupMenu.
3. Call PopupMenu's Show method to display the pop-up menu.
Code Case:
Click on a button to pop up the menu item
@Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; } @SuppressLint ("Newapi") Public voidGetItem (view view) {//creating pop-up menusPopupMenu PopupMenu =NewPopupMenu (mainactivity. This, view); //Get Menu ObjectMenu Menu =Popupmenu.getmenu (); //Add menu OptionsMenu.add ("Find"); Menu.add (Delete); Popupmenu.show ();//Show Menu//set a listener event for a menu item, toast the contents of a clicked menu itemPopupmenu.setonmenuitemclicklistener (NewOnmenuitemclicklistener () {@Override Public BooleanOnmenuitemclick (MenuItem Item) {Toast.maketext (mainactivity. This, Item.gettitle (), 0). Show (); return false; } }); }
Three ways to create menus (menu)--android Development Road 2