First, let's define LayoutInflater. Layoutinflater is used to instantiate an xml layout file as a View object.
There are three methods to get the Layoutinflater object. There are no more tricks. It works. After tracking the source code, we find that the three methods call context in essence. getSystemService (), so we recommend that you use context later. getSystemService (). The statement is as follows:
[Java]
LayoutInflater inflater = context. getSystemService (Context. LAYOUT_INFLATER_SERVICE );
LayoutInflater inflater = context. getSystemService (Context. LAYOUT_INFLATER_SERVICE); Next we will focus on the use of the inflate () method:
1. Return Value:
With the View object, we can obtain the layout of items in the setContentView () or getView () method in the Custom Adapter.
2. parameters:
[Java]
Public View inflate (int resource, ViewGroup root)
Public View inflate (int resource, ViewGroup root)
The first parameter is the R index of an xml file, such as R. layout. activity_main. If the second parameter is not null, it is to put the obtained View object into a Viewgroup container. In this case, some readers are not very clear. At the end of the article, an example is provided to illustrate the problem.
[Java]
Public View inflate (int resource, ViewGroup root, boolean attachToRoot)
Public View inflate (int resource, ViewGroup root, boolean attachToRoot)
The third parameter indicates that if the second parameter is not null, true is placed in the Viewgroup referenced by the second parameter, if it is false, it is not placed in the Viewgroup of the second parameter.
The following uses an instance to describe the problem.
There are two layout files:
Activity_main.xml
[Html]
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: paddingBottom = "@ dimen/activity_vertical_margin"
Android: paddingLeft = "@ dimen/activity_horizontal_margin"
Android: paddingRight = "@ dimen/activity_horizontal_margin"
Android: paddingTop = "@ dimen/activity_vertical_margin"
Android: orientation = "vertical">
<TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello_world"/>
<RelativeLayout
Android: id = "@ + id/viewgroup"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content">
</RelativeLayout>
</LinearLayout>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: paddingBottom = "@ dimen/activity_vertical_margin"
Android: paddingLeft = "@ dimen/activity_horizontal_margin"
Android: paddingRight = "@ dimen/activity_horizontal_margin"
Android: paddingTop = "@ dimen/activity_vertical_margin"
Android: orientation = "vertical">
<TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello_world"/>
<RelativeLayout
Android: id = "@ + id/viewgroup"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content">
</RelativeLayout>
</LinearLayout> inflatertest. xml
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
<CheckBox
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "inflatedemo"/>
</LinearLayout>
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
<CheckBox
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "inflatedemo"/>
</LinearLayout> when writing in an Activity:
[Java]
<PRE class = java name = "code"> @ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
MViewGroup = (RelativeLayout) findViewById (R. id. viewgroup );
MInflater = (LayoutInflater) getApplicationContext (). getSystemService (
Context. LAYOUT_INFLATER_SERVICE );
MInflater. inflate (R. layout. inflatertest, mViewGroup );
} </PRE>
<PRE> </PRE>
Or
<PRE> </PRE>
[Java]
@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mViewGroup = (RelativeLayout) findViewById (R. id. viewgroup); mInflater = (LayoutInflater) getApplicationContext (). getSystemService (Context. LAYOUT_INFLATER_SERVICE); mInflater. inflate (R. layout. inflatertest, mViewGroup) ;}@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
MViewGroup = (RelativeLayout) findViewById (R. id. viewgroup );
MInflater = (LayoutInflater) getApplicationContext (). getSystemService (
Context. LAYOUT_INFLATER_SERVICE );
MInflater. inflate (R. layout. inflatertest, mViewGroup );
}
Or
[Java]
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
MViewGroup = (RelativeLayout) findViewById (R. id. viewgroup );
MInflater = (LayoutInflater) getApplicationContext (). getSystemService (
Context. LAYOUT_INFLATER_SERVICE );
MInflater. inflate (R. layout. inflatertest, mViewGroup, true );
}
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
MViewGroup = (RelativeLayout) findViewById (R. id. viewgroup );
MInflater = (LayoutInflater) getApplicationContext (). getSystemService (
Context. LAYOUT_INFLATER_SERVICE );
MInflater. inflate (R. layout. inflatertest, mViewGroup, true );
} The result is as follows:
When writing in an Activity:
[Java]
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
MViewGroup = (RelativeLayout) findViewById (R. id. viewgroup );
MInflater = (LayoutInflater) getApplicationContext (). getSystemService (
Context. LAYOUT_INFLATER_SERVICE );
MInflater. inflate (R. layout. inflatertest, mViewGroup, false );
}
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
MViewGroup = (RelativeLayout) findViewById (R. id. viewgroup );
MInflater = (LayoutInflater) getApplicationContext (). getSystemService (
Context. LAYOUT_INFLATER_SERVICE );
MInflater. inflate (R. layout. inflatertest, mViewGroup, false );
} The result is as follows:
Today, we are here to learn about the framework and understand the meaning of function return values and parameters, and learn to summarize the rules.