Monkeys can read the Android menu summary

Source: Internet
Author: User

Simply talk about the use of the Android menu menu to help students get started.

Menu Main Categories:

Options Menu : The most general menu
submenu : One of the implementation scenarios for multi-tiered menus
context Menu : The menu that appears after long pressing the view control in Android

Options menu (Optionmenu)

The menu is displayed by default in the upper-right corner, with two layouts, XML layout, and Java code layout, first looking at how the XML is laid out
A simple code example

static layout (XML):
    <group        android:id="@+id/group1"        android:checkableBehavior="single"        android:visible="true">        <item            android:id="@+id/item1"            android:orderInCategory="100"            android:title="111" />        <item            android:id="@+id/item2"            android:orderInCategory="99"            android:title="222" />    </group>

Create a click event in Java code that listens to the menu (override the following two methods)

  @Override  public  boolean          Oncreateoptionsmenu  (Menu menu) {getmenuinflater (). Inflate (R.menu.menu_first,menu); return     true ; }  @Override  public  boolean  onoptionsitemselected  (MenuItem item) {if  (!item.ischecked ()) item.setchecked (true );        else  item.setchecked (false ); return     true ; }

Of course, the item can be added to the menu in addition to being embedded in the group, which is not shown here. There are a number of methods for item and group, the method name is basically clear, here are a few ways to pay special attention:
android:checkableBehavior="single", the method sets the selection type of item, and all and none two selection types, corresponding

Note that there is a Showasaction method to control the display of the item, please be sure to add before use, xmlns:app="http://schemas.android.com/apk/res-auto" the method mainly has a few options, crossing know

Dynamic Layout (Java):

You can add the menu option by Menu.add in the overridden Oncreateoptionsmenu method.
Demo:

        menu.add("333");        menu.add(1,1,1,"香蕉");        menu.add(1,2,2,"苹果");

Add the above code. Parameters: (int,int,int,string) refer to Groupid,itemid,ordercaterary and Itemtext respectively. The first directly sets the Itemtext to exist alone.

Remember that setting icon in the default hidden item cannot be displayed

Sub-menu (submenu)

Feeling has been abandoned, in the test will not be as long ago as the display of a suspended submenu style, the actual effect is as follows:

Implementation code:
Add the following code to the oncreateoptionsmenu :

        int Base=1; Submenu submenu = Menu.addsubmenu (Base,Base+1, Menu.none,"Fruit Encyclopedia"); Submenu.add (Base,Base+1,Base+1,"Apple"); Submenu.add (Base,Base+2,Base+2,"Banana"); Submenu.add (Base,Base+3,Base+3,"Orange");

So, how do you do the following in XML? The answer is nested, yes, the code will be known at a glance

    <item android:title="Fruit Daquan">        <menu >            <item android:title="Banana"></Item>            <item android:title="Apple"></Item>            <item android:title="Orange"></Item>        </Menu>    </Item>


Context menu (ContextMenu)

Take a TextView click time as an example, long press the display context menu

Implementation process:
First register the View

        TextView textView = (TextView)findViewById(R.id.textView);        this.registerForContextMenu(textView);

Rewrite oncreatecontextmenu and oncontextitemselected

 @Override     Public void Oncreatecontextmenu(ContextMenu menu, View V, Contextmenu.contextmenuinfo menuinfo) {Menu.setheadertitle ("Hello");        Menu.setheadericon (R.mipmap.ic_launcher); Menu.add (1,1,3,"Banana"); Menu.add (1,2,1,"Pineapple"); Menu.add (1,3,1,"Apple"); }@Override     Public Boolean oncontextitemselected(MenuItem Item) {return Super. oncontextitemselected (item); }

The biggest difference between a context menu and an option menu is that the owner of the Options menu is activity, and the owner of the context menu is the view in the activity. Each activity has and has only one options Menu, which serves the entire activity. While an activity often has multiple view, not every view has a context menu, which requires us to display the registerforcontextmenu (view view) to specify.

OK, very simple, do not post code, if it is useful to you, welcome to the bottom of the likes, there are questions can be commented on the form of the proposed.

Monkeys can read the Android menu summary

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.