Android advanced tutorial (6)-Use of menuinflater in Android (layout definition menu)

Source: Internet
Author: User

Hello everyone, the last section describes the use of layoutinflater. In this section, I will talk about menuinflater. As the name suggests, layoutinflater is used to parse layout files defined in layout, so does menuinflater resolve the menu layout file defined in the menu directory? Congratulations! (* ^__ ^ *) Xi ......

In the traditional sense, the menu definition is complicated. When we use menuinflater to generate a menu, you will find that it is so elegant. Haha, my demo today defines four menus, A menu event is implemented. Click the setting menu to enter the mobile phone setting status! Let's take a look:

 

Use of menuinflater (layout definition menu) "width =" 380 "Height =" 557 "> Use of menuinflater (layout definition menu) "width =" 379 "Height =" 556 ">

The detailed steps for implementing the demo are as follows:

1. Create an android project named menuinflaterdemo.

2. Create the menu directory under the res directory and create the options_menu.xml (the menu we define) file. The Code is as follows:

<? XML version = "1.0" encoding = "UTF-8"?>
<Menu
Xmlns: Android = "http://schemas.android.com/apk/res/android">
<Item Android: Id = "@ + ID/menu_add"
Android: Title = "add"
Android: icon = "@ Android: drawable/ic_menu_add"
/>
<Item Android: Id = "@ + ID/menu_wallaper"
Android: Title = "Wallpaper"
Android: icon = "@ Android: drawable/ic_menu_gallery"
/>
<Item Android: Id = "@ + ID/menu_search"
Android: Title = "Search"
Android: icon = "@ Android: drawable/ic_search_category_default"
/>
<Item Android: Id = "@ + ID/menu_setting"
Android: Title = "Settings"
Android: icon = "@ Android: drawable/ic_menu_preferences"
/>
</Menu>
<? XML version = "1.0" encoding = "UTF-8"?>
<Menu
Xmlns: Android = "http://schemas.android.com/apk/res/android">
<Item Android: Id = "@ + ID/menu_add"
Android: Title = "add"
Android: icon = "@ Android: drawable/ic_menu_add"
/>
<Item Android: Id = "@ + ID/menu_wallaper"
Android: Title = "Wallpaper"
Android: icon = "@ Android: drawable/ic_menu_gallery"
/>
<Item Android: Id = "@ + ID/menu_search"
Android: Title = "Search"
Android: icon = "@ Android: drawable/ic_search_category_default"
/>
<Item Android: Id = "@ + ID/menu_setting"
Android: Title = "Settings"
Android: icon = "@ Android: drawable/ic_menu_preferences"
/>
</Menu>

Iii. Main class menuinflaterdemo. Java code. Little code is written here. I only wrote the RESPONSE event of the fourth menu (settings) here. All the code is as follows:

Package com. Android. tutor;
Import Android. App. activity;
Import Android. content. intent;
Import Android. OS. Bundle;
Import Android. View. Menu;
Import Android. View. menuinflater;
Import Android. View. menuitem;
Public class menuinflaterdemo extends activity {
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
}
         
@ Override
Public Boolean oncreateoptionsmenu (menu ){
Menuinflater Inflater = getmenuinflater ();
Inflater. Inflate (R. Menu. options_menu, menu );
Return true;
}
         
@ Override
Public Boolean onoptionsitemselected (menuitem item ){
Switch (item. getitemid ()){
Case R. Id. menu_add:
                 
Break;
Case R. Id. menu_wallaper:
Break;
Case R. Id. menu_search:
Break;
Case R. Id. menu_setting:
Showsettings ();
Break;
}
Return super. onoptionsitemselected (item );
}
         
         
Private void showsettings (){
             
Final intent settings = new intent (Android. provider. settings. action_settings );
Settings. setflags (intent. flag_activity_new_task |
Intent. flag_activity_reset_task_if_needed );
                 
Startactivity (settings );
}
}
Package com. Android. tutor;
Import Android. App. activity;
Import Android. content. intent;
Import Android. OS. Bundle;
Import Android. View. Menu;
Import Android. View. menuinflater;
Import Android. View. menuitem;
Public class menuinflaterdemo extends activity {
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
}
      
@ Override
Public Boolean oncreateoptionsmenu (menu ){
Menuinflater Inflater = getmenuinflater ();
Inflater. Inflate (R. Menu. options_menu, menu );
Return true;
}
      
@ Override
Public Boolean onoptionsitemselected (menuitem item ){
Switch (item. getitemid ()){
Case R. Id. menu_add:
     
Break;
Case R. Id. menu_wallaper:
Break;
Case R. Id. menu_search:
Break;
Case R. Id. menu_setting:
Showsettings ();
Break;
}
Return super. onoptionsitemselected (item );
}
      
      
Private void showsettings (){
       
Final intent settings = new intent (Android. provider. settings. action_settings );
Settings. setflags (intent. flag_activity_new_task |
Intent. flag_activity_reset_task_if_needed );
           
Startactivity (settings );
}
}

4. Run the code. Click the menu button on the simulator!

OK ~ We are here today. Have a good weekend !!!!

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/fengkuanghun/archive/2010/07/23/5758288.aspx

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.