Android Menu Brief Analysis (Optionsmenu)

Source: Internet
Author: User

Android menu mechanism, before and after Android 3.0 have a great go, Android 3.0 introduced ActionBar, navigation UI Interaction has a great change, but the menu logic and interface is still the same. Here the main introduction of the next Android menu creation, use, followed by the introduction of Actionbar,sherlockactionbar with the use of menus, custom menu creation.

There are 4 main types of Android menus:

Optionmenu

ContextMenu

Popmenu

Submenu

Let's start with the following optionmenu, which creates menus using XML files and JAVA code, respectively.

1.OptionMenu

Options menu, pop-up when you click the Menu key, select the item or click another area, the menu disappears.

The implementation of the related methods of Optionmenu in Activity, the related methods are:


@Overridepublic boolean Oncreateoptionsmenu (Menu menu) {    log.d (tag, "Oncreateoptionsmenu");    return Super.oncreateoptionsmenu (menu);} @Overridepublic boolean Onprepareoptionsmenu (Menu menu) {    log.d (tag, "Onprepareoptionsmenu");    return Super.onprepareoptionsmenu (menu);} @Overridepublic void onoptionsmenuclosed (Menu menu) {    System.out.println ("onoptionsmenuclosed");    LOG.D (Tag, "onoptionsmenuclosed");    Super.onoptionsmenuclosed (menu);} @Overridepublic boolean onoptionsitemselected (MenuItem item) {    switch (Item.getitemid ()) {case    R.id.action_ Save:        log.d (Tag, "Action_save");        break;    Case r.id.action_settings:        log.d (Tag, "action_settings");        break;    Case R.id.action_delete:        log.d (Tag, "Action_delete");        break;    Case R.id.action_edit:        log.d (Tag, "Action_edit");        break;    }    return super.onoptionsitemselected (item);}

Oncreateoptionmenu, executed only once during the Activity life cycle, Onprepareoptionmenu Every time the menu is opened , When the menu disappears, execute the Onoptionsmenuclosed method, and the menu is selected to execute onoptionsitemselected.

Here's a look at the Optionmenu created with XML on the Android 2.3 system, as described below.




First: 4 menu options, no icons

Second: 8 menu items, with icons

Third: Click More, no icon

The same code, if not compatible (minimum version >11) before Android 3.0 version, what is the effect on Android4.4?








First: 4 menu options, no icons, show only two, one more logo

Second: Click More menu items

The third: 8 menu items, there are icons, but the menu shows no text, more menu items only text

Four: Click More, no icon

Here's a look at the specific code

1. Writing Menu.xml



<menu xmlns:android= "http://schemas.android.com/apk/res/android" ><item android:id= "@+id/action_share" android:orderincategory= "0" android:showasaction= "ifroom" android:icon= "@android:d rawable/ic_menu_share" Android: title= "Share"/><item android:id= "@+id/action_save" android:orderincategory= "1" android:showasaction= "IfRoom     "Android:icon=" @android:d rawable/ic_menu_save "android:title=" save "/><item android:id=" @+id/action_search " android:orderincategory= "2" android:showasaction= "Ifroom" android:icon= "@android:d rawable/ic_menu_search" and roid:title= "Search"/><item android:id= "@+id/action_delete" android:orderincategory= "4" android:showAsAction = "Ifroom" android:icon= "@android:d rawable/ic_menu_delete" android:title= "delectaction"/> <item android:id= " @+id/action_edit "android:orderincategory=" 5 "android:showasaction=" Ifroom "android:icon=" @android:d Rawable/ic_me Nu_edit "Android:title=" EDit "/> <item android:id=" @+id/action_help "android:orderincategory=" 6 "android:showasaction=" IfRoom "a ndroid:icon= "@android:d rawable/ic_menu_help" android:title= "Help"/> <item android:id= "@+id/action_compass "android:orderincategory=" 6 "android:showasaction=" Ifroom "android:icon=" @android:d Rawable/ic_menu_compass "a Ndroid:title= "Compass"/> </menu>

Android:orderincategory the location of the menu item in the menu


2. Add a menu to Activity



@Overridepublic boolean Oncreateoptionsmenu (Menu menu) {    Menuinflater inflater = Getmenuinflater ();    LOG.D (Tag, "Oncreateoptionsmenu");    Inflater.inflate (R.menu.main_icon, menu);    return Super.oncreateoptionsmenu (menu);}

3. Response to Menu events


    @Overridepublic boolean onoptionsitemselected (MenuItem item) {    switch (Item.getitemid ()) {case    R.id.action_ Save:        log.d (Tag, "Action_save");        break;    Case r.id.action_settings:        log.d (Tag, "action_settings");        break;    Case R.id.action_delete:        log.d (Tag, "Action_delete");        break;    Case R.id.action_edit:        log.d (Tag, "Action_edit");        break;    Case R.id.action_search:        log.d (Tag, "Action_search");        break;    }    return super.onoptionsitemselected (item);}

This completes the creation, use, and response of the menu.

Android is officially recommended to create menus using XML, and you can also create menus in JAVA code. The implementation process is as follows

A. Adding a menu item directly in the Oncreateoptionsmenu () method


@Overridepublic boolean Oncreateoptionsmenu (Menu menu) {menuinflater inflater = Getmenuinflater (); Menu.add (int groupid,int itemid,int orderid,string title) method parameter//add method returns a MenuItem object that can add an icon and invoke the SetIcon () method Log.    D (Tag, "Oncreateoptionsmenu"); Menu.add (0, Menu.first+1, menu.first+1, Menusstrings[0]). SetIcon (Android.    R.drawable.ic_menu_add); Menu.add (0, menu.first+2, menu.first+2, Menusstrings[1]). SetIcon (Android.    R.drawable.ic_menu_agenda); Menu.add (0, Menu.first+3, menu.first+3, menusstrings[2]). SetIcon (Android.    R.drawable.ic_menu_call); Menu.add (0, Menu.first+4, menu.first+4, Menusstrings[3]). SetIcon (Android.    R.drawable.ic_menu_close_clear_cancel); Menu.add (0, Menu.first+5, menu.first+5, Menusstrings[4]). SetIcon (Android.    R.drawable.ic_menu_crop); Menu.add (0, Menu.first+6, menu.first+6, Menusstrings[5]). SetIcon (Android.    R.drawable.ic_menu_upload);    Inflater.inflate (R.menu.main, menu); return Super.oncreateoptionsmenu (menu);}

B. Response menu Check Event

Respond to selected events the same way as above, no more code is posted ~.


About Optionsmenu The introduction of this, we have a problem in the comments Exchange AH


The next article to introduce the use of Contexmenu


Resources:

Official Handbook

EoE Menu Wiki

Reference Blog Menu Introduction

Reference Blog Menu Introduction






Android Menu Brief Analysis (Optionsmenu)

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.