Dynamic LayoutInflater and layoutinflater
LayoutInflater is used to instantiate layout xml layout files as View objects. LayoutInflater is similar to findViewById (). The difference is that LayoutInflater is used to find and instantiate xml layout files in the layout folder! FindViewById () is used to find the widget controls (such as buttons and TextView) in a specific xml file ).
Three methods for obtaining the LayoutInflater instance,
1. LayoutInflater inflater = getLayoutInflater (); // call the getLayoutInflater () of the Activity ()
2. LayoutInflater inflater = LayoutInflater. from (this );
3. LayoutInflater inflater = (LayoutInflater) Context. getSystemService (LAYOUT_INFLATER_SERVICE );
GetLayoutInflater ():
The getLayoutInflater () method of the Activity is to call the getLayoutInflater () method of PhoneWindow. Let's take a look at the source code:
Public PhoneWindow (Context context) {super (context); mLayoutInflater = LayoutInflater. from (context );}
We can see that it actually calls LayoutInflater. from (context ).
LayoutInflater. from (context ):
Public static LayoutInflater from (Context context ){
LayoutInflater =
(LayoutInflater) context. getSystemService (Context. LAYOUT_INFLATER_SERVICE );
If (LayoutInflater = null ){
Throw new AssertionError ("LayoutInflater not found .");
}
Return LayoutInflater;
}
We can see that it actually calls context. getSystemService ().
Conclusion: The three methods are called Context. getSystemService ().
After LayoutInflater is instantiated, the xml layout file of layout is instantiated as a View object.
1. LayoutInflater inflater = getLayoutInflater (); View view = inflater. inflate (R. layout. ID, null );
2. LayoutInflater inflater = LayoutInflater. from (this); View view = inflater. inflate (R. layout. ID, null );
3. LayoutInflater inflater = (LayoutInflater) Context. getSystemService (LAYOUT_INFLATER_SERVICE); View view = inflater. inflate (R. layout. ID, null );
Inflate Method
Through the sdk api documentation, you can know that this method has the following overload forms, and the returned values are View objects, as follows:
The public View inflate (int resource, ViewGroup root) (commonly used) inflate () method generally receives two parameters. The first parameter is the layout id to be loaded, the second parameter refers to nesting a parent layout for the exterior of the layout, and directly passing null if not needed. In this way, a layout instance is successfully created and then added to the specified position.
Public View inflate (XmlPullParser parser, ViewGroup root) public View inflate (XmlPullParser parser, ViewGroup root, boolean attachToRoot) public View inflate (int resource, ViewGroup root, boolean attachToRoot)
LayoutInflater inflater = (LayoutInflater) getSystemService (LAYOUT_INFLATER_SERVICE );
View view = inflater. inflate (R. layout. custom, (ViewGroup) findViewById (R. id. test ));
// EditText editText = (EditText) findViewById (R. id. content );
// Error EditText editText = (EditText) view. findViewById (R. id. content );
For the code above, specify the second parameter ViewGroup root. Of course, you can also set it to null.
Demo:
The following is a very simple example to show more intuitively how to use LayoutInflater. For example, there is a project, and the layout file corresponding to MainActivity is activity_main.xml. The Code is as follows:
1.
<LinearLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
2.
android:id=
"@+id/main_layout"
3.
android:layout_width=
"match_parent"
4.
android:layout_height=
"match_parent"
>
5.
6.
</LinearLayout>
The layout file has only one blank LinearLayout and has no controls in it. Therefore, nothing should be displayed on the interface.
Next, let's define a layout file named button_layout.xml. The Code is as follows:
1.
<Button xmlns:android=
"http://schemas.android.com/apk/res/android"
2.
android:layout_width=
"wrap_content"
3.
android:layout_height=
"wrap_content"
4.
android:text=
"Button"
>
5.
6.
</Button>
This layout file is also very simple, with only one Button.
Now we need to find a way to add the button_layout layout to the LinearLayout of the main layout file through LayoutInflater. Modify the code in MainActivity according to the usage just introduced, as shown below:
01.
public
class
MainActivity
extends
Activity {
02.
03.
private
LinearLayout mainLayout;
04.
05.
@Override
06.
protected
void
onCreate(Bundle savedInstanceState) {
07.
super
.onCreate(savedInstanceState);
08.
setContentView(R.layout.activity_main);
09.
mainLayout = (LinearLayout) findViewById(R.id.main_layout);
10.
LayoutInflater layoutInflater = LayoutInflater.from(
this
);
11.
View buttonLayout = layoutInflater.inflate(R.layout.button_layout,
null
);
12.
mainLayout.addView(buttonLayout);
13.
}
14.
15.
}
We can see that the LayoutInflater instance is obtained first, its inflate () method is called to load the layout of button_layout, And the addView () method of LinearLayout is called to add it to LinearLayout.
Now you can run the program. The result is as follows:
The Button is displayed on the page! It means that we have successfully added the button_layout layout to LinearLayout with LayoutInflater. LayoutInflater is widely used when you need to dynamically add views. For example, LayoutInflater is often seen in ScrollView and ListView.