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