How to Create Menu 3: Android Development 2, menuandroid

Source: Internet
Author: User

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 ;}});}

 

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.