How to Create Menu 3: Android Development 2, menuandroid
Three menu creation methods
I. OptionsMenu --- option menu
Menus in Android applications are hidden by default. Menus are displayed only when you click the MENU key on your mobile phone. This Menu is called an Options Menu or a system Menu. Android 3.0 does not require the MENU button to be provided on mobile devices. Therefore, Android recommends using ActionBar instead of Menu. The menu display effects vary in different versions.
Ii. ContextMenu --- context menu
3. PopupMenu --- pop-up menu
You can set a pop-up menu on a specified component. By default, the pop-up menu is displayed below or above the component. PopupMenu can add multiple menu items, you can also add sub-menu items for menu items.
Procedure:
1. Call new PopupMenu (MainActivity. this, View view). view indicates the component that inspires the pop-up menu.
2. Call the MenuInflater () method to fill the menu with PopupMenu.
3. Call the show method of PopupMenu to display the pop-up menu.
Code case:
Click a button to bring up the menu items
@ Override public boolean onCreateOptionsMenu (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 void getItem (View view) {// create a pop-up menu PopupMenu popupMenu = new PopupMenu (MainActivity. this, view); // obtain the Menu object menu Menu = popupMenu. getMenu (); // Add menu options menu. add ("Search"); menu. add ("delete"); popupMenu. show (); // display the menu // set the listening event of the menu item, and popupMenu of the menu item clicked by Toast. setOnMenuItemClickListener (new OnMenuItemClickListener () {@ Override public boolean onMenuItemClick (MenuItem item) {Toast. makeText (MainActivity. this, item. getTitle (), 0 ). show (); return false ;}});}