Android Learning Note--menu (iii)

Source: Internet
Author: User

    • Knowledge points

Today continue yesterday did not finish the menu study, mainly is the popup menu study.

    • Popup menu (pop-up menus)

A pop-up menu is a menu model that is fixed on a view. Mainly used in the following three cases:

    1. Provides an overflow style (Overflow-style) menu for specific content.
    2. Other parts of the command sentence, such as the Add button, can be used to provide different add operations with a popup menu.
    3. Provides a drop-down menu similar to spinner but does not maintain a durable selection.

How do you show pop-up menus?

If you define a menu in an XML file, the following three steps can be displayed:

1. Use the PopupMenu constructor to instantiate a pop-up menu that requires a fixed view of the context and menu for the current application.

2. Use Menuinflater to populate your menu resources into the Menus object, this menu object is returned by Popupmenu.getmenu (in API 14 and above can be replaced with Popupmenu.inflater)

3. Call Popupmenu.show ()

Here's an example to understand the use of PopupMenu:

1  Public voidShowPopup (View v) {2PopupMenu popup =NewPopupMenu ( This, v);3Menuinflater Inflater =Popup.getmenuinflater ();4 inflater.inflate (R.menu.popup, Popup.getmenu ());5Popup.setonmenuitemclicklistener ( This);6 popup.show ();7     }8 9 @OverrideTen      Public BooleanOnmenuitemclick (MenuItem arg0) { One         Switch(Arg0.getitemid ()) { A          Caser.id.item1: -Toast.maketext ( This, "You have clicked the item 1", Toast.length_long). Show (); -              Break; the          Caser.id.item2: -Toast.maketext ( This, "You have clicked the item 2", Toast.length_long). Show (); -              Break; -          Caser.id.item3: +Toast.maketext ( This, "You have clicked the Item 3", Toast.length_long). Show (); -              Break; +         default: A              Break; at         } -         return false; -}
View Code
1 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 android:orientation= "vertical"6    >7 8     <TextView9         Android:layout_width= "Wrap_content"Ten Android:layout_height= "Wrap_content" One Android:text= "@string/clickme"  A Android:onclick= "ShowPopup" - android:clickable= "true"/> -      the     <ImageButton -         Android:layout_width= "Wrap_content" - Android:layout_height= "Wrap_content" - android:src= "@drawable/ic_launcher" + android:clickable= "true" - Android:onclick= "ShowPopup"  /> +       A </LinearLayout>
View Code

Android Learning Note--menu (iii)

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.