Three ways to create menus (menu)--android Development Road 2

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.