Android Menu Easy to create

Source: Internet
Author: User

There are 4 classes related to menu in Android:

Menu: The parent window for menus, which is used to create a menu, a parent interface such as Submenu,contentmenu,menuitem, Submenuyo for creating submenus, Contentmenu for creating context menus, MenuItem is used to create a menu item.

Menu Development Basic ideas: 1, with Oncreateoptionsmenu (Menu menu) (For options menus, or with Ncreatecontentmenu (Contentmenu Contentmenu,view Source,contentmenuinfo info) For the context menu , create a menu, and invoke the Menu object's method in the change method to add an item or submenu; 2. Rewrite activity's onoptionsmenuselected () (For Options menu, oncontentitemselected () method, for context menu) method corresponding event.

There are 3 types of menus:

One, the Options menu (Normal menu)

The so-called normal menu, click the menu button to pop-up menus, simple to create Options menu, the code is as follows:  

public class Mainactivity extends Appcompatactivity {
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
}
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Getmenuinflater (). Inflate (R.menu.mainmenu,menu);//Create a menu from an XML file, where R.menu.mainmenu asks the menu file to add the defined XML file.

Menu.add (0,0,0, "menu Test");//Add a menu option with code
Menu.add (0,1,0, "Text settings");
Submenu submenu = Menu.addsubmenu (0,6,0, "Sub-menu Test");//Adding a submenu
Submenu.add (0,2,0, "I am submenu 1");//Add a submenu item
Submenu.add (0,3,0, "I am submenu 2");
Submenu.add (0,4,0, "I am submenu 3");
return true;
}
@Override
public boolean onoptionsitemselected (MenuItem item) {
return super.onoptionsitemselected (item);
Switch (Item.getitemid ()) {
Case 0:
Toast.maketext (This, "menu test", Toast.length_short). Show ();
Break
Case 1:
Toast.maketext (This, "Text settings", Toast.length_short). Show ();
Break
Case 2:
Toast.maketext (This, "I am submenu 1", Toast.length_short). Show ();
Break
Case 3:
Toast.maketext (This, "I am submenu 2", Toast.length_short). Show ();
Break
Default
Toast.maketext (This, "Who am I?") ", Toast.length_short). Show ();
Break
}
return true;
}
}


Second, the context menu

A menu that pops up when a control is long pressed is called a context menu (like a menu that pops up with a right mouse button), and you must register a context menu with the control in advance:

Add a button to the Activity_main.xml file and register it with the context menu.

1. Register the context menu for the control that you want to register the context menu for, such as registerforcontextmenu (button);

2. Rewrite the Oncreatecontentmenu () method and create a menu item within the method;

3, rewrite the oncontentitemselected () method to handle menu click events,

The simple example code is as follows:

  

public class Mainactivity extends Appcompatactivity {
Button btn;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
BTN = (Button) Findviewbyid (R.id.button);
Registerforcontextmenu (BTN);
}

@Override
public void Oncreatecontextmenu (ContextMenu menu, View V, contextmenu.contextmenuinfo menuinfo) {
Super.oncreatecontextmenu (menu, V, menuinfo);
if (v.getid () = = R.id.button) {
Menu.add (0, 0, 0, "context menu Test");
Menu.add (0, 1, 0, "context menu Test 1");
Menu.add (0, 2, 0, "context menu Test 2");

Menu.setgroupcheckable (0, True, true);//Set 3 menu items to a single-selection menu item
Menu.setheadertitle ("Select background color"); Set the caption, icon for the context menu
Menu.setheadericon (R.DRAWABLE.TT); Setting the context menu icon
}
}

@Override
public boolean oncontextitemselected (MenuItem item) {
return super.oncontextitemselected (item);
Switch (Item.getitemid ()) {
Case 0:
Toast.maketext (This, "context menu Test", Toast.length_short). Show ();
Break
Case 1:
Toast.maketext (This, "context menu Test 1", Toast.length_short). Show ();
Break
Case 2:
Toast.maketext (This, "context menu Test 2", Toast.length_short). Show ();
Break
Default
Break
}
return true;
}
}

Three, pop-up menu:

PopupMenu represents a pop-up menu that pops up on the specified component and appears above or below the component. Sensory function can be replaced with listdialog, do not know which is better.

There are 2 ways to create a menu in Android:

First, using XML to define the menu

When you define a menu file in the Res/menu directory, such as a Menu1.xml file, the menu file must have a <menu> root node,<menu> tag without any attributes, but can be nested in <item> to represent a submenu.

Three tags commonly used in the menu <menu>, <item>, <group>

1.1 <item> represents a menu option that cannot be nested, and has several common properties as follows:

The 1.1.1 ID represents the resource ID of the menu option,

1.1.2 Tittle setting menu item title

1.1.3 Android:icon Setting menu item icon

1.1.4 android:checkable, sets whether the menu item is optional;

1.1.5 android:checked, sets whether the menu item is selected

1.1.5android:visibale set menu item whether courseware;

1.2 <group> elements for creating a set of menu items to use when creating a submenu, Common properties

Android:checkablebehavior sets whether a submenu item belongs to a single or multiple selection, and the value can be "single" and so on.

1.3 Developing a basic path

1.3.1 Create an XML file, make a new androidsourcedirection, Name menu, and create a good XML file,

1.3.2 overrides the activity's Oncreateoptionsmenu () or Oncreatecontentmenu () method and constructs the menu through XML in the method through the Getmenuinflater (). Inflate () method. ,

1.3.3 overrides the Onoptionselected () method or the Oncontentitemselected () method for the corresponding event.

1.4 Code

      

<?xml version= "1.0" encoding= "Utf-8"?>
<menu xmlns:android= "Http://schemas.android.com/apk/res/android" >
<item android:id = "@+id/menuid1"
Android:title = "Test 1"/>
<item android:id = "@+id/menuid2"
Android:title = "Test 1"/>
</menu>

public class Mainactivity extends Appcompatactivity {
Button btn;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
BTN = (Button) Findviewbyid (R.id.button);
Registerforcontextmenu (BTN);
}

@Override
public void Oncreatecontextmenu (ContextMenu menu, View V, contextmenu.contextmenuinfo menuinfo) {

Menuinflater inflator=new Menuinflater (this); Instantiate a Menuinflater object
Inflator.inflate (R.menu.testmenu, menu); Add the menu resource corresponding to the XML
Menu.setheadericon (R.drawable.color); Setting the context menu icon
Menu.setheadertitle ("Please select the background color"); Set context menu Title
}
@Override
public boolean oncontextitemselected (MenuItem item) {
return super.oncontextitemselected (item);
Switch (Item.getitemid ()) {
Case 0:
Toast.maketext (This, "context menu Test", Toast.length_short). Show ();
Break
Case 1:
Toast.maketext (This, "context menu Test 1", Toast.length_short). Show ();
Break
Case 2:
Toast.maketext (This, "context menu Test 2", Toast.length_short). Show ();
Break
Default
Break
}
return true;
}
}

Android Menu Easy to create

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.