Preface
Layoutinflater usage
Layoutinflater is a view object used to instantiate an XML layout.
When the application is running, the layout file in the resource will be loaded in advance. If there are many resources in the layout, the performance will be affected. Therefore, you can select the layoutinflater method to load it, this reduces the workload of running applications.
Public View inflate (INT resource, viewgroup root)
Fill in a new view from the specified XML Resource
Parameter resource: the XML layout ID to be loaded, such as R. layout. list_item.
Parameter root: parent view, optional, generally null
Public static layoutinflater from (context)
Obtain layoutinflater from the given context
You can obtain layoutinflater in the following three ways:
First:
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View myView = inflater.inflate(R.layout.main, null);
The second method is the same as the first method, that is, the from method encapsulates getsystemservice (...) and so on.
LayoutInflater inflater = LayoutInflater.from(context);
View myView = inflater.inflate(R.layout.main, null);
Third: The getlayoutinflater () method is the activity method. In the final analysis, it is still the first method.
LayoutInflater inflater = getLayoutInflater();
View myView = inflater.inflate(R.layout.main, null);
So we can use setcontentview to directly set the layout when loading the layout, and then use findviewbyid () to obtain the Control ID.
For example, we can load the main. xml layout file in four ways. However, layoutinflater is generally used in listview and other places,
For example, the getview () method of baseadapter will use: http://www.cnblogs.com/loulijun/archive/2011/12/28/2305016.html
Package com. loulijun. demo6;
Import Android. App. activity;
Import Android. content. context;
Import Android. OS. Bundle;
Import Android. View. layoutinflater;
Import Android. View. view;
Import Android. widget. Button;
Import Android. widget. textview;
Public class demo6activity extends activity {
Private button BTN;
Private textview TV;
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
// ---------- Method 1 ----------
// Setcontentview (R. layout. Main );
// BTN = (button) findviewbyid (R. Id. BTN );
// TV = (textview) findviewbyid (R. Id. TV );
// ---------- Method 2 ----------
// Layoutinflater Inflater = getlayoutinflater ();
// ---------- Method 3 ----------
// Layoutinflater Inflater = layoutinflater. From (this );
// ---------- Method 4 ----------
Layoutinflater Inflater = (layoutinflater) This. getsystemservice (
Context. layout_inflater_service );
View view = Inflater. Inflate (R. layout. Main, null );
Setcontentview (View );
BTN = (button) view. findviewbyid (R. Id. BTN );
TV = (textview) view. findviewbyid (R. Id. TV );
BTN. setonclicklistener (New button. onclicklistener ()
{
@ Override
Public void onclick (view v ){
//...
}
});
}
Use setcontentview (R. layout. main) after the layout is set, the layout will be immediately displayed, and the layout file loaded using the inflate () method will get a view object, and you can setcontentview (View) as needed. Generally, you only need to setcontentview in the activity. If it is not acitivity, you need to use layoutinflater to dynamically load control controls.
Menuinflater usage
Menuinflater is used to load the menu layout file, which is similar to the preceding
Similar to layoutinflater, layout files in resources are pre-loaded when the application is running. If there are many resources in the menu layout, the performance will be affected. Therefore, you can select menuinflater to load the files when using it, this reduces the workload of running applications.
Compared with layoutinflater, menuinflater is much easier to use. It only uses the activity. getmenuinflater () method.
For example:
1. Create the menu folder under the res directory, and create mymenu. XML in it
<? XML version = "1.0" encoding = "UTF-8"?>
<Menu xmlns: Android = "http://schemas.android.com/apk/res/android">
<Item
Android: Title = "phone"
Android: icon = "@ Android: drawable/ic_menu_call"
Android: Id = "@ + ID/action_call"/>
<Item
Android: Title = "photography"
Android: icon = "@ Android: drawable/ic_menu_camera"
Android: Id = "@ + ID/action_camera"/>
<Item
Android: Title = "add"
Android: icon = "@ Android: drawable/ic_menu_add"
Android: Id = "@ + ID/action_add"/>
<Item
Android: Title = "delete"
Android: icon = "@ Android: drawable/ic_menu_delete"
Android: Id = "@ + ID/action_delete"/>
</Menu>
2. Rewrite the oncreateoptionsmenu method and onoptionsitemselected method, and then use menuinflater to load the layout in oncreateoptionsmenu.
Package com. loulijun. demo6;
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. View. Menu;
Import Android. View. menuinflater;
Import Android. View. menuitem;
Import Android. widget. Button;
Import Android. widget. textview;
Public class demo6activity extends activity {
Private button BTN;
Private textview TV;
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
}
@ Override
Public Boolean oncreateoptionsmenu (menu ){
Menuinflater Inflater = getmenuinflater ();
// Fill the menu
Inflater. Inflate (R. Menu. mymenu, menu );
Return super. oncreateoptionsmenu (menu );
}
@ Override
Public Boolean onoptionsitemselected (menuitem item ){
Switch (item. getitemid ())
{
Case R. Id. action_add:
// Operation
Break;
Case R. Id. action_call:
Break;
Case R. Id. action_camera:
Break;
Case R. Id. action_delete:
Break;
}
Return super. onoptionsitemselected (item );
}
}
Running result: