Android Inflater usage

Source: Internet
Author: User

In actual development, the layoutinflater class is very useful, and its function is similar to findviewbyid (). The difference is that layoutinflater is used to find and instantiate XML layout files under Res/layout/, while findviewbyid () is to find the specific widget controls (such as button and textview) under the XML layout file ). Specific functions: 1. layoutinflater. Inflate () is required for loading an interface that is not loaded or that is to be dynamically loaded;

2. For a loaded interface, you can use the activiyt. findviewbyid () method to obtain the interface elements.

Layoutinflater is an abstract class, which is declared as follows in the document:

Publicpolicactclass
Layoutinflater extends
Object

Three methods for obtaining a layoutinflater instance

1.Layoutinflater Inflater = getlayoutinflater (); // call the getlayoutinflater () of the activity ()

2.Layoutinflater localinflater = (layoutinflater) Context. getsystemservice


(Context. layout_inflater_service );

3.Layoutinflater Inflater = layoutinflater. From (context );

In fact, these three methods are essentially the same, as can be seen from the source code:

Getlayoutinflater ():

Getlayoutinflater () of the activity ()
The method is to call the getlayoutinflater () method of phonewindow. Let's take a look at the source code:

Public
Phonewindow (context) {super (context );
Mlayoutinflater = layoutinflater. From (context );}

We can see that it actually calls layoutinflater. From (context ).

Layoutinflater. From (context ):

PublicStatic
Layoutinflater from (context) {layoutinflater = (layoutinflater)
Context. getsystemservice (context. layout_inflater_service );
If
(Layoutinflater =Null)
{ThrowNew
Assertionerror ("layoutinflater not found .");
}
Return
Layoutinflater ;}

We can see that it actually calls context. getsystemservice ().

Conclusion: The three methods are called context. getsystemservice ().

Inflate MethodThrough the sdk api documentation, you can know that this method has the following overload forms, and the return values are
View object, as follows:

Public
View inflate (int
Resource, viewgroup root) 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)

Sample Code:

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.

Note:

· The inflate method is different from the findviewbyid method;

· Inflater is used to find and instantiate XML layout files under Res/layout;

· Findviewbyid () is used to find widgets (such as buttons and textview) in a specific XML layout file ).

 

Source: http://www.cnblogs.com/shitianzeng/articles/2323427.html

Source:

Related Article

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.