Android--Menu

Source: Internet
Author: User

Use in an activityMenu

I don't know if you remember, when you created your first Android project in the previous chapter ,ADT was in helloworldactivity A Oncreateoptionsmenu () method is automatically created in the . This method is used to create the menu in the activity, because at that time our focus is not here, so directly ignored, now can be carefully analyzed.

After all, the phone is different from the computer, its screen space is very limited, so the full use of screen space in the mobile phone interface design is very important. If you have a large number of menus in your activity that need to be displayed, then the interface design will be awkward, because only these menus can occupy nearly one-third of the screen space, what should I do? Don't worry,Android gives us a way to get the menu to show up without taking up any screen space.

First inResCreate a new directory underMenufolder, right-clickRescatalog →New→Folder, enter a folder nameMenu, clickFinish. And then create a new name in this folderMainmenu file, right-clickMenufolder →New→Android XML File,2.9is shown.

Figure 2.9

Enter mainin the file name and click Finish to create it. Then add the following code in the main.xml :

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

<item

Android:id= "@+id/add_item"

android:title= "Add"/>

<item

Android:id= "@+id/remove_item"

android:title= "Remove"/>

</menu>

Here we create two menu items, where the <item> tag is used to create a specific menu item and then Assign a unique identifier to the menu item by Android:id, by android:title Specify a name for this menu item.

Then open firstactivity, overriding the oncreateoptionsmenu () method, as shown in the following code:

public boolean Oncreateoptionsmenu (Menu menu) {

Getmenuinflater (). Inflate (R.menu.main, menu);

return true;

}

PassGetmenuinflater ()method to getMenuinflaterobject, and then call itsInflate ()method to create a menu for the current activity. Inflate ()method receives two parameters, the first parameter specifies which resource file we use to create the menu, which of course passes inR.menu.main, the second parameter specifies which of our menu items will be added toMenuobject, this is used directlyOncreateoptionsmenu ()method that is passed in theMenuparameters. And then give this method backtrueThat allows the created menu to be displayed if it returns afalse, the created menu will not be displayed.

Of course, just letting the menu show is not enough, we define the menu not only for the sake of viewing, but also for the menu to be really usable, so we have to define the menu response event. rewrite the onoptionsitemselected () method in firstactivity :

public boolean onoptionsitemselected (MenuItem item) {

Switch (Item.getitemid ()) {

Case R.id.add_item:

Toast.maketext (This, "You clicked Add", Toast.length_short). Show ();

Break

Case R.id.remove_item:

Toast.maketext (This, "You clicked Remove", Toast.length_short). Show ();

Break

Default

}

return true;

}

In the onoptionsitemselected () method, by calling Item.getitemid () to determine which menu item we clicked on, and then adding our own logic processing to each menu item, here we ingenious, Pop up a Toast that just learned .

Rerun the program and press the Menu key, as shown in the effect 2.10 .

Figure 2.10

As you can see, the menu is not displayed by default, only the menu will be displayed at the bottom, so we can use the menu with confidence because it will not occupy any active space. Then click on the Add menu item, as shown in the effect 2.11 .

Android--Menu

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.