android--Creating a menu in an activity

Source: Internet
Author: User

In the mobile phone, the screen is limited, when you have a large number of menus need to be displayed in the activity, the interface design will be difficult and not good-looking, you need to have a menu to help.

Create menu First step:

If your Res folder does not have a menu folder, right-click the Res directory-new-directory, enter the folder named menu, and click OK. Then right-click the folder New-menu resource file to create a menu document called Main, click OK to finish.

Create Menu Second step:

Write the following code in Main.xml: set two options in the menu.

Create the third step of the menu:

Write the following code in Mainactivity:

One can get a Menuinflater object by calling the Getmenuinflater () method, and then call its Inflater () method to create a menu for the current activity.

Of course, the menu only shows up in the event, and we need to define a response event for it.

To create the menu fourth step:

just below the code, write the following code:

override the Onoptionitemselected () method, whose parameters represent the item you have selected, and then use the Getitemid () method to get the item ID, which item is determined by case. Here is a simple toast to distinguish the results when different options are selected.

When you click Add, the Add string is returned and ADD_FF is returned when you click Add_ff.

android--Creating a menu in an activity

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.