Layoutinflater must not be unfamiliar, will know it is mainly used to load the layout of
Because the task of loading the layout is usually called Setcontentview () in the activity
The interior of the Setcontentview () method is implemented using Layoutinflater. , but this part of the source code is internal , it is not easy to find
1. Get an example of Layoutinflater
Law 1,
Layoutinflater Layoutinflater = Layoutinflater. from (context);
Law 2,
Layoutinflater layoutinflater = (layoutinflater) context. Getsystemservice(context.layout_inflater_service);
In fact, the first is the second simple way of writing, just Android gave us a package . Once you have an instance of Layoutinflater, you can call its inflate () method to load the layout.
Layoutinflater.inflate (resourceId, Root);
The inflate () method generally receives two parameters,
The first parameter is the layout ID to be loaded
The second parameter is to give the layout the outer layer of a nested parent layout , and null if it is not required. This succeeds in successfully creating an instance of the layout and then adding it to the specified location to be displayed.
Mainlayout. AddView (Buttonlayout);
Layoutinflater technology is widely used in the need to dynamically add view , such as in ScrollView and ListView, often can see layoutinflater figure.
Layoutinflater is actually using the pull parsing method provided by Android to parse the layout file.
Createviewfromtag () This method, and the node name and parameters are passed in. Seeing this method name, we should be able to guess that it is used to create a view object from a node name. Indeed, within the Createviewfromtag () method, the CreateView () method is called, and the instance of the view is created and returned using reflection.
LinearLayout's parent layout is really a framelayout, and this framelayout is automatically added by the system to help us.
The layout we passed in when we called the Setcontentview() method was actually put into this framelayout , which is why this method is called Setcontentview(), Instead of calling Setview().
android--custom Controls (i) Layoutinflater