Android Common menu:
1.OptionsMenu.
2.ContextMenu.
3.android4.0 after the PopupMenu and Actionmode.
Optionsmenu
1. Create:
1.1 Override Oncreateoptionsmenu in activity where Optionsmenu is required (menu
menu) method and returns True (otherwise, ContextMenu will not be displayed).
1.2 In Oncreateoptionsmenu (Menu menu), add us to the menu object in the parameter
menu options that need to be set. There are two ways to add menu options:
1) Add a menu button via the Add method of the Menu object.
2) By injecting the menu definition file in the Res/menu, add the Menus button.
1.3 Now we have the ContextMenu.
2. Properties:
Android:title name;
Android:id ID value;
Android:orderincategory menu sort;
Android:showasaction Menu Display Mode:
Ifroom: Appears in item, but is hidden in the overflow list if it already has 4 or more than 4 item. Of course, the number is not only limited to 4, depending on the width of the screen
Never: Never show. Only appears in the overflow list, and only the title is displayed, so it's best to take the title with you when you define the item.
Always: It will be displayed regardless of overflow.
The Withtext:withtext value indicates the action bar to display the text caption. The Action bar displays the caption as much as possible, but if the icon is valid and is limited by the action bar space, the text caption may not be displayed.
Collapseactionview (4.1 and below the simulator is not effective): declares that the Operation window should be folded into a button, when the user selects this button, the Operation window expands. Generally used in conjunction with Ifroom.
In onoptionsitemselected (MenuItem item), you can hear the Optionsmenu click event to get the object MenuItem of the menu button you clicked.
3. Add Sub-menu
Method 1: We can add a new menu tag to the menu item to add a submenu.
Method 2: Add a submenu to the menu by Addsubmenu in Oncreateoptionsmenu (Menu menu).
public class Optionsmenuactivity extends Activity {@Overridepublic boolean Oncreateoptionsmenu (Menu menu) {/*//1. Add a menu via Java Code//For Menu Add Option menu.add ("Search");//groupid option group number; ItemId option ID; order sequence ordinal; menu.add (0, 5, 0, "Setting");// Add a submenu for menu submenu more = Menu.addsubmenu ("more"), More.add ("More1"), More.add ("More2"), *///2. Add menu Getmenuinflater () via XML resource file. Inflate (R.menu.menu, menu); System.out.println ("Oncreateoptionsmenu");//returns True to show Optionsmenu, return Falseoptionsmenu will not be displayed. return true;} @Overridepublic boolean Onprepareoptionsmenu (Menu menu) {System.out.println ("Onprepareoptionsmenu");// Delete Search option Menu.removeitem (r.id.menu_search); return Super.onprepareoptionsmenu (menu);} /** * @see android.app.activity#onoptionsitemselected (android.view.MenuItem) */@Overridepublic Boolean Onoptionsitemselected (MenuItem Item) {//Response click event String title = (string) item.gettitle (); if ("Search". Equals (title)) {} else if ("Setting". Equals (title)) {}else if ("more". Equals (title)) {}system.out.println ("MenuItem =" +title);//This returns TRUE or FALSE, which we can all respond to. Used to indicate that we disposed of this event (true), false means we do not handle it. return false;}}
TIPS:
Their click-through monitoring is in the onoptionsitemselected;
Oncreateoptionsmenu is only called once, and is called only when it is created. Need to call the Onprepareoptionsmenu () method, here to modify the Optionmenu State and button;
Above 4.0, we must call Invalidateoptionsmenu () before we can call Onprepareoptionsmenu ().
Float Contextual Menu
This is a floating menu that requires a long press trigger.
1. Create:
1.1 Creating a menu We need to implement a method in the activity as well:
Oncreatecontextmenu (ContextMenu menu, View v,contextmenuinfo menuinfo) and create a menu in it.
1.2 Trigger method
Registerforcontextmenu (view view) binds this menu to a view.
Oncontextitemselected (MenuItem Item).
PopupMenu
Provides an action similar to the right mouse button menu under Window
1. Create the object:
PopupMenu Pmenu = new PopupMenu (this, view);
2. Injection interface:
Pmenu.getmenuinflater (). Inflate (R.menu.menu, Pmenu.getmenu ());
3. Add a Listener
Pmenu.setonmenuitemclicklistener (New Popupmenu.onmenuitemclicklistener () {
@Override
public boolean Onmenuitemclick (MenuItem item) {
return false;
}
});
4. Display: Pmenu.show ();
5. Manual cancellation: Pmenu.dismiss ();
Android Basics (ii)--menu