android--custom Controls (i) Layoutinflater

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.