Learn about Android Ax (6)

Source: Internet
Author: User

The menu includes the Options menu (Optionmenu), the context menu (ContextMenu), and the submenu (submenu).

Steps to create a submenu (submenu):

1, rewrite the activity of the Oncreateoptionmenu (Menu menu) method, in this method through the Menu object method to add menus.


2. Response menu click event, overriding Activity onoptionitemselected (MenuItem mi) method


The code is as follows:

public class Mainactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Create submenu submenu Submenu1=menu.addsubmenu ("submenu 1");// Set the header of the menu header Submenu1.setheadertitle ("Heading 1 of the menu header");//Set menu icon Submenu1.seticon (r.drawable.ic_launcher);// Set the icon for the menu header Submenu1.setheadericon (r.drawable.ic_launcher),//Add the menu item to the 0 group Submenu1.add (0, 0, 0, "menu item 1"), Submenu1.add (0, 1, 0, "menu item 2"); Submenu1.add (0, 2, 0, "menu item 3"); return true;   @Overridepublic boolean onoptionsitemselected (MenuItem Item) {//TODO auto-generated method Stubtoast (Item.gettitle ()) ; return super.onoptionsitemselected (item);} private void Toast (charsequence title) {//TODO auto-generated method Stubtoast.maketext (Mainactivity.this,title, Toast.length_short). Show ();}}

When you click menu:



Click Sub-Menu 1:


Learn about Android Ax (6)

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.