Android menu Design (1): Create options menu Using xml file Layout

Source: Internet
Author: User

1. Preparations

 

Download several images to make the menu a icing on the cake

2. Create an android Project

 

Create a folder named menu under the res file of the project to place the xml file. That is, the layout file of the menu.

 

The directory structure is as follows:

 

 

Game_menu.xml source code:

View plain
<? Xml version = "1.0" encoding = "UTF-8"?>
<Menu xmlns: android = "http://schemas.android.com/apk/res/android">
<Item android: id = "@ + id/new_game"
Android: icon = "@ drawable/ic_new_game"
Android: title = "new_game"/>

<Item android: id = "@ + id/help"
Android: icon = "@ drawable/ic_help"
Android: title = "help"/>
</Menu>
 

Update_menu.xml source code:

View plain
<? Xml version = "1.0" encoding = "UTF-8"?>
<Menu xmlns: android = "http://schemas.android.com/apk/res/android">
<Item android: id = "@ + id/new_game"
Android: icon = "@ drawable/back_game"
Android: title = "exit"/>

<Item android: id = "@ + id/help"
Android: icon = "@ drawable/exit_game"
Android: title = "back"/>
</Menu>
 

3. Create a menu

 

Override the onCreateOptionsMenu (Menu menu) method.

View plain
@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
Log. d (TAG, "onCreateOptionsMenu () is involed! "+ (Times ++) +" th ");
MenuInflater mInflater = getMenuInflater ();
MInflater. inflate (R. menu. game_menu, menu );
// Equivalent code below
// Return super. onCreateOptionsMenu (menu );
Return true; // if false is returned, the menu is not displayed.
}
 

OK, run the program, and click "menu". The result is as follows:

 

 

4. Design click events for menus

 

Override the onOptionsItemSelected (MenuItem item) method!

View plain
@ Override
Public boolean onOptionsItemSelected (MenuItem item ){
// Handle item selection
Switch (item. getItemId ()){
Case R. id. new_game:
// NewGame ();
Return true;
Case R. id. help:
// ShowHelp ();
Return true;
Default:
Return super. onOptionsItemSelected (item );
}
}
 

5. dynamically change menu

 

The onCreateOptionsMenu (Menu menu) method is called only once when the App is running, which is equivalent to the onCreate () method of the Activity. To change the menu dynamically, you must override the onPrepareOptionsMenu (Menu menu) method.

View plain
@ Override
Public boolean onPrepareOptionsMenu (Menu menu ){
Log. d (TAG, "onPrepareOptionsMenu () is involed! "+ (Times ++) +" th ");
MenuInflater mInflater = getMenuInflater ();
MInflater. inflate (R. menu. update_menu, menu );
// Equivalent code below
// Return super. onPrepareOptionsMenu (menu );
Return true; // if false is returned, the menu is not displayed.
}
 

The display effect is as follows:

 

 

Look at the log, right ??!!!

 

 

 

OK. Now you understand that the onPrepareOptionsMenu (Menu menu Menu) method is called after the onCreateOptionsMenu (menu) method. Then we click Menu again and click it several more times. Effect and Log, as shown below.

 

 

 

 

 

It can be found that the onPrepareOptionsMenu (Menu menu) method is called every time you click the menu. If you do this in the actual code, there will be a lot of menu items, obviously not, what should we do? The sample code is as follows:

View plain
@ Override
Public boolean onPrepareOptionsMenu (Menu menu ){
Log. d (TAG, "onPrepareOptionsMenu () is involed! "+ (Times ++) +" th ");
// Clear menu
Menu. clear ();
MenuInflater mInflater = getMenuInflater ();
MInflater. inflate (R. menu. update_menu, menu );
// Equivalent code below
// Return super. onPrepareOptionsMenu (menu );
Return true; // if false is returned, the menu is not displayed.
}
 

In this case, the menus created in the onCreateOptionsMenu (Menu menu) method will disappear ,:

 

 

 

6. Prompt

 

At the end, make a prompt in the original text above the sdk api document:

View plain
On Android 2.3 and lower, the system callonprepareoptionsmenu () each time the user opens the Options Menu.
On Android 3.0 and higher, you must call invalidateOptionsMenu () when you want to update

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.