Two ways to create an Android menu and add an icon to a menu item

Source: Internet
Author: User

Two ways to create a menu:


1. Create a menu in the XML file:


Specific code:

<menu xmlns:android= "Http://schemas.android.com/apk/res/android"

Xmlns:tools= "Http://schemas.android.com/tools"

tools:context= "Com.xunfang.menucreate.MainActivity" >

//Create sub-menu here

<item

Android:id= "@+id/sms"

android:icon= "@drawable/sms"

android:orderincategory= "100"

Android:showasaction= "Never"

android:title= "SMS" >

<menu>

<item

Android:id= "@+id/smsfamily"

android:icon= "@drawable/smsalt"

android:orderincategory= "100"

Android:showasaction= "Never"

android:title= "Home sms"/>

<item

Android:id= "@+id/smscompany"

android:icon= "@drawable/twitteralt"

android:orderincategory= "100"

Android:showasaction= "Never"

android:title= "Company SMS"/>

</menu>

</item>

<item

Android:id= "@+id/camera"

android:icon= "@drawable/camera"

android:orderincategory= "100"

Android:showasaction= "Never"

android:title= "Camera"/>

</menu>

In this case, when creating a menu, you need to use the item tag to create a menu option, you must use an item

When you need to create a submenu : As part of the background color, create a submenu under the main menu, you need to nest a menu tag in item, then write a submenu in the Menu tab, and each submenu is written in the Item tab.


2. Create a menu dynamically:

public boolean Oncreateoptionsmenu (Menu menu) {

Create a submenu for a file

submenu sub = Menu.addsubmenu (Menu.first, 1, 1, "file");

Sub.add (menu.first+3, 10, 1, "hidden Group 3");

Sub.add (menu.first+3, 11, 2, "display Group 3");

menu.add (menu.first,2,2, "edit");

Sub.seticon (r.drawable.browser);

Sub.setheadericon (r.drawable.browser);

MenuItem refactor = menu.add (Menu.first + 1,3,10, "refactoring");

Refactor.seticon (r.drawable.clock);

MenuItem Source = menu.add (Menu.first + 1,4,6, "source code");

Source.seticon (r.drawable.sms);

menu.add (Menu.first + 2, 5, 5, "navigation");

menu.add (Menu.first + 2,6,3, "search");

return true;

}

The four parameters in the Add () method above are: The first parameter indicates that it is in the same group

The second parameter represents a unique ID

The third parameter is an ID that represents the order in which the menu item is displayed in the menu

The fourth parameter represents the displayed text


Note: Add an icon to the menu item:


private void Seticonvisible (Menu menu) {


try {

Class clazz = Class

. forname ("Com.android.internal.view.menu.MenuBuilder");

Method m = Clazz.getdeclaredmethod ("Setoptionaliconsvisible",

Boolean.class);

m.setaccessible (true);

M.invoke (menu, true);

} catch (Exception e) {

e.printstacktrace ();

}

}

The background part is the key part of the code, the above method is my own add a method, in the Add icon only need to call this method on the line.

This article is from the "Beginner" blog, so be sure to keep this source http://10154241.blog.51cto.com/10144241/1663962

Two ways to create an Android menu and add an icon to a menu item

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.