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

Source: Internet
Author: User

Hello everyone, what we talked about in the previous section is:Layoutinflater
And I will talk about this section.Menuinflater
As the name suggests,Layoutinflater
Is used to parse the definition inLayout
The layout file under, thenMenuinflater
Is it used to parse the definition inMenu
What about the menu layout file under the directory? Congratulations! (* ^__ ^ *) Xi ......

 

The definition menu in the traditional sense is complicated.Menuinflater
To generate a menu, you will find how refreshing it is. Haha, my demo today defines four menus and implements a menu event. ClickSet
(Setting
) Menu to enter the mobile phone setting status! Let's take a look:

 


 

Below is the implementationDemo
Detailed steps:

 

1. CreateAndroid
Project name:Menuinflaterdemo
.

 

2.Res
Create under directoryMenu
Directory, and createOptions_menu.xml
(The menu we define) file, the Code is as follows:

 

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

 

Iii. Main classMenuinflaterdemo. Java
The code is rarely written here. I only wrote the fourth menu (Settings
). All code is as follows:

 

Package COM. android. tutor; <br/> Import android. app. activity; <br/> Import android. content. intent; <br/> Import android. OS. bundle; <br/> Import android. view. menu; <br/> Import android. view. menuinflater; <br/> Import android. view. menuitem; <br/> public class menuinflaterdemo extends activity {<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/>}</P> <p> @ override <br/> Public Boolean oncreateoptionsmenu (menu) {<br/> menuinflater Inflater = getmenuinflater (); <br/> Inflater. inflate (R. menu. options_menu, menu); <br/> return true; <br/>}</P> <p> @ override <br/> Public Boolean onoptionsitemselected (menuitem item) {<br/> switch (item. getitemid () {<br/> case R. id. menu_add: </P> <p> break; <br/> case R. id. menu_wallaper: <br/> break; <br/> case R. id. menu_search: <br/> break; <br/> case R. id. menu_setting: <br/> showsettings (); <br/> break; <br/>}< br/> return Super. onoptionsitemselected (item); <br/>}</P> <p> private void showsettings () {</P> <p> final intent settings = new intent (Android. provider. settings. action_settings); <br/> Settings. setflags (intent. flag_activity_new_task | <br/> intent. flag_activity_reset_task_if_needed); </P> <p> startactivity (settings); <br/>}< br/>}

 

4. Run the code and clickMenu
The above button will appear!

 

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

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.