Android Menu Development

Source: Internet
Author: User

Menu Category:

Options Menu (Optionsmenu)context Menu (ContextMenu)Sub-menu (submenu)
Pop-up menu ( Popup )

 
first of all, Options Menu (Optionsmenu)

First, the method is introduced:

Public Booleanoncreateoptionsmenu (Menu menu) : Use this method to invoke the Optionsmenu .

Public booleanonoptionsitemselected (MenuItem Item) : The action that occurs after the menu item is selected.

Public voidonoptionsmenuclosed (Menu menu): the action that occurs when the menu is closed.

Public Booleanonprepareoptionsmenu (Menu menu) : The Options menu is displayed before Onprepareoptionsmenu method is called, you can use this method to adjust the menu according to the situation.

Public booleanonmenuopened (int featureid, menu menu) : The action that occurs after the opening of the singles.

Second, default style

The default style is to pop up a menu at the bottom of the screen, which we'll call him. Options MenuOptionsmenu, in general, the Options menu displays a maximum of2rows per row3menu items that have text icons and are also known asIcon Menus, if more than6items, which will be hidden from the beginning of item sixth, will appear in the sixth item More, click MoreItem Sixth and subsequent menu items, which are also known asExpanded Menus. described below.

1. Overloaded Oncreateoptionsmenu (Menu menu) method
@Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action Bar if it is pre Sent.//getmenuinflater (). Inflate (R.menu.main, menu); Menu.add (0,1,1, "settings"). SetIcon (r.drawable.setting); Menu.add ( 0,3,3, "Settings"). SetIcon (r.drawable.setting); Menu.add (0,2,2, "Download"). SetIcon (R.drawable.download); }

overloads the Oncreateoptionsmenu (Menu menu) method, and adds a menu item to this method, and finally returns true if False, the menu is not displayed.

Attention:
 most phones will have a "Menu "after an app is installed on your phone, you canMenu "displays the menu associated with the app.  however, fromAndroid 3.0Start,Androidno longer required on mobile devices to provideMENUcase, although there are still many mobile phones that will provideMENUkeys, but some are no longer available. In this case,AndroidRecommended UseActionBarto replace the menu. In a future blog post, we will introduceAndroidtheActionBarthe support

4.2on theOptionsmenuis put inActionbarabove, must be inXMLfileset inshowasaction= "Always"stay alive.showasaction="Ifroom"to be inActionbaron the display. Only inActionBaricon is displayed on the menu. To set in the code.,Menu.finditem(ID).setshowasaction(menuitem.show_as_action_always)



2. Overloaded onoptionsitemselected (MenuItem Item) method to capture menu item events

@Overridepublic boolean onoptionsitemselected (MenuItem Item) {//TODO auto-generated method Stubif (item.getgroupid () = = 0 &&item.getitemid () = = 1) {Intent Intent = new Intent (this, secondactivity.class); startactivity (Intent);} Else{toast.maketext (this, Item.gettitle (), Toast.length_short). Show (); return super.onoptionsitemselected (item);}


context Menu (ContextMenu)
When the user presses a control , the popup menu is called the context menu. We often Right-click the popup menu in Windows is the context menu.

1. Oncreatecontextmenu () method of heavy load Activity , call Menu The Add method adds a menu item MenuItem

@Overridepublic void Oncreatecontextmenu (ContextMenu menu, View v,contextmenuinfo menuinfo) {  menu.add (0, 1, 0, " Red Background ");    Menu.add (0, 2, 0, "green background");    Menu.add (1, 3, 0, "white background");  TODO auto-generated Method Stubsuper.oncreatecontextmenu (menu, V, menuinfo);}


2. overload oncontextitemselected () method, Response menu click event

@Overridepublic boolean oncontextitemselected (MenuItem Item) {//TODO auto-generated Method stub  switch ( Item.getitemid ()) {case    1:    mytext.setbackgroundcolor (color.red);     break;    Case 2:    mytext.setbackgroundcolor (color.green);     break;    Case 3:    mytext.setbackgroundcolor (color.white);     break;    } return true;}


3. Reload the registerforcontextmenu () method to register the context menu for the view

    Private TextView myText; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main); myText = (TextView) Findviewbyid (R.id.mytext); Registerforcontextmenu (MyText);  }
Sub-menu (submenu)
A submenu is a menu of multi-level display of groupings of the same functionality, such as the " new", "open", "Close" submenu in Windows ' File menu.

Ways to create submenus

1 overloaded activity oncreateoptionsmenu () method, call menu addsubmenu () method Add submenu items

2, call the submenu add () rice, add sub-menu items

@Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action Bar if it is pre Sent. Submenu submenu = Menu.addsubmenu (0, 4, 4, "Login/Register"), Submenu.add (1, 1, 1, "Login"), Submenu.add (1, 2, 2, "register"); return true;


3. overloaded onoptionsitemselected (MenuItem Item) method to capture menu item events

This method has a MenuItem parameter that can be used to determine which menu item is clicked by using its GetTitle and Getitemid methods.
@Overridepublic boolean onoptionsitemselected (MenuItem Item) {//TODO auto-generated method Stubif (Item.gettitle (). Equals ("Login")) {LOG.E ("Action:", "click Register/Login");} return super.onoptionsitemselected (item);}


submenu is a submenu of the menu, and after you add submenu, you can add its submenu items through the Submenu.add method. You cannot display an image on a submenu item, but you can display an image on the head of a submenu, but a submenu item can have a check box and an option button.
Note: Submenus can no longer be added to sub-menus;

Pop-up menu ( Popup
This type of menu needs to be bound to a view that pops up from the top or bottom of the view (depending on the screen space) when the view is clicked. How to use:1. Create an XML menu resource file;
Popup.xml
<menu xmlns:android= "Http://schemas.android.com/apk/res/android" >    <item        android:id= "@+id/ Action_edit "        android:orderincategory=" android:showasaction= "        never"        android:title= "edit"/>        <item        android:id= "@+id/action_share"        android:orderincategory= "        android:showasaction" = "Never"        android:title= "popup"/> </menu>


2~5 Step can be implemented in the Click event of the Binding view! 2, establish the PopupMenu object, instantiate the context and the bound view;3, using the PopupMenu object Getmenuinflater (). Inflate () Press the XML menu file in,4, use the PopupMenu object's own Setonmenuitemclicklistener set the click event;5. Show pop-up menu using PopupMenu object's own show.
public void Showpopumenu (View v) {PopupMenu popup = new PopupMenu (mainactivity.this, v);    Menuinflater inflater = Popup.getmenuinflater ();    Inflater.inflate (R.menu.popup, Popup.getmenu ());    Popup.show ();}

demo:http://download.csdn.net/detail/q610098308/9236383


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Menu Development

Related Article

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.