Widgets (6, menu)

Source: Internet
Author: User

Menu is the Android menubar control, which is triggered when the Phone menu key is selected, the basic function is as follows:

 Public Boolean Oncreateoptionsmenu (Menu menu) {    = menu.add (Menu.none, Menu_id2, Menu.none, "Item 2"    ); = Menu.add (Menu.none, MENU_ID3, Menu.none, "menu item 3");     = Menu.add (Menu.none, Menu_id4, Menu.none, "menu item 4");     = Menu.add (Menu.none, MENU_ID5, Menu.none, "menu item 5");

To handle menu item selection events in Onoptionselectitem:

    @Override    publicboolean  onoptionsitemselected (MenuItem item) {                 if (Item.getitemid () =//The Itemid here corresponds to the second parameter            when initializing the menu = Toast.maketext(This, "menu item 1") , toast.length_short);            T.show ();

Menu support adds level two menus, as follows:

submenu sub = Menu.addsubmenu (Menu.none, Menu_id6, Menu.none, "menu item 6 >>"//Parameter with Menu.add method    Sub.seticon (r.drawable.ic_launcher);        Sub.add (0, 60, 0, "Sub-menu 6.1");          Sub.add (

Menu can add an icon to MenuItem through SetIcon, but the version after android4.0 needs to be set iconenable:

Clazz = Class.forName ("Com.android.internal.view.menu.MenuBuilder"boolean. ) Class);  M.setaccessible (true); M.invoke (menu,true// execute setoptionaliconsvisiblesub.seticon (r.drawable.ic_ Launcher);

Widgets (6, menu)

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.