inflater-Layout Conversion Implementation

Source: Internet
Author: User

In layman's words, inflate is the equivalent of locating a layout defined in an XML. Because if you use Findviewbyid () directly in an activity, the component in the layout of Setconentview () is the corresponding one. so if you use other layout in your activity, such as the layout on the dialog, you also have to set the contents of the Layout on the dialog box (like picture ImageView, Text TextView), you have to use inflate () Find the layout of the dialog box first, and then use the layout object to find the components above it, such as:

View view = View.inflate (thisnull= (TextView) View.findviewbyid (R.ID.DIALOG_TV); Dialogtv.settext ("ABCD");

If the component R.id.dialog_tv is a component on the dialog box, and you use This.findviewbyid directly (R.ID.DIALOG_TV), you will definitely get an error.

There are three ways to generate Layoutinflater:

Layoutinflater inflater = Layoutinflater.from (this =the This. Getsystemservice ( Layout_inflater_service);


Then call the inflate method to turn the XML layout file into view
Public View Inflate (int resource, ViewGroup root, Boolean attachtoroot)

In the view class, there are also inflate methods
public static View Inflate (context context, int resource, ViewGroup root)

Layoutinflater.inflate ()

In fact, these three kinds of ways are essentially the same, from the source can be seen:

Getlayoutinflater ():

The Getlayoutinflater () method of the Activity is to call Phonewindow's Getlayoutinflater () method and take a look at the source code:

 
 Public Phonewindow (Context context) {          Super(context);                 = Layoutinflater.from (context);  

It can be seen that it is actually called layoutinflater.from (context).

Layoutinflater.from (context):

 Public Static Layoutinflater from (context context) {         =   (layoutinflater) Context.getsystemservice ( Context.layout_inflater_service);        if NULL {           thrownew assertionerror ("Layoutinflater not found.") );       }        return Layoutinflater;   

It can be seen that it actually calls Context.getsystemservice ().

Conclusion: So the final essence of these three methods is all called Context.getsystemservice ().

Inflate method
Through the SDK API documentation, you can know that the method has the following overload forms, the return value is the View object, as follows:

 
 Public View Inflate (int public View Inflate (xmlpullparser parser, ViewGroup root)     Public Boolean attachtoroot)     Public View Inflate (intBoolean

Schematic code:

 
Layoutinflater Inflater = (layoutinflater) getsystemservice (layout_inflater_service);    View view == (EditText) Findviewbyid (r.id.content); // Error  = (EditText) View.findviewbyid (r.id.content);

  

For the above code, the second parameter ViewGroup root is specified, and of course you can set the null value.


Attention:

Inflate method is different from Findviewbyid method;

Inflater is used to find the XML layout file under Res/layout, and instantiate it;

Findviewbyid () is to find specific widget controls (such as Button, TextView, and so on) in the specific XML layout file.

inflater-Layout Conversion Implementation

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.