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:
- Provides an overflow style (Overflow-style) menu for specific content.
- Other parts of the command sentence, such as the Add button, can be used to provide different add operations with a popup menu.
- 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)