Preface
This chapter is about android. view. MenuInflater. The version is Android 2.3 r1 and translated from "zookeeper". You are welcome to visit his blog: http://www.cnblogs.com/mxgsa/. thank you again for "zookeeper "! I look forward to your participation in Android Chinese API translation, contact me over140@gmail.com.
Statement
You are welcome to repost, but please keep the original source of the article :)
Blog Garden: http://www.cnblogs.com/
Android Chinese translation group: http://goo.gl/6vJQl
Body
I. Structure
Public class MenuInflater extends Object
Java. lang. Object
Android. view. MenuInflater
Direct subclass
TabActivity
Ii. Overview
This class is used to instantiate the menu XML file as a menu object.
Due to performance, too many menus may cause a heavy burden because the program loads some pre-processed XML files during creation. Therefore, MenuInflater cannot be used for running more than one XmlPullParser xml file. It can only use the compiled resources returned by one XmlPullParser (R. Some files)
Iii. Constructor
Public MenuInflater (Context context)
Create a menu for filling
See
getMenuInflater()
Iv. Public Methods
Public voidInflate(Int menuRes, Menu menu)
The menu layer is filled with a specified xml resource.InflateException
。
Parameters
MenuRes: The resource ID in the XML layout file to be loaded (for exampleR.menu.main_activity
)
The items and sub-menus to be filled are added to the menus to be filled.
5. Supplement
Article Selection
How to Create a MenuInflater Android menu from xml
MenuInflater instance in Android
Use of MenuInflater in Android (layout definition menu)
Sample Code
Create a project, project file list
MenuInfalterTest. java
Public class MenuInflaterTest extends Activity {
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
}
Public boolean onCreateOptionsMenu (Menu menu ){
// Obtain the current menu
MenuInflater inflater = getMenuInflater ();
// Fill the menu
Inflater. inflate (R. menu. option_menu, menu );
Return true;
}
/**
* Processing menu click events
*/
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 );
}
/**
* Display setting options
*/
Private void showSettings (){
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 );
}
}
}
Main. xml <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"
/>
</LinearLayout>
Option_menu.xml
<? 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>