Acquisition and use of Layoutinflater

Source: Internet
Author: User

It is still very useful to layoutinflater this class in real-world development, which acts like Findviewbyid (). The difference is that Layoutinflater is used to find the XML layout file under res/layout/and is instantiated, while Findviewbyid () is looking for specific widget controls (such as button, TextView, and so on) under the XML layout file.
Specific role:
1, for a non-loaded or want to dynamically load the interface, you need to use Layoutinflater.inflate () to load;

2, for an already loaded interface, you can use the Activiyt.findviewbyid () method to obtain the interface elements.


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

Public abstract class Layoutinflater extends Object

Three ways to get Layoutinflater instances

Layoutinflater inflater = Getlayoutinflater ();  Call Activity Getlayoutinflater () Layoutinflater localinflater =  (layoutinflater) Context.getsystemservice (Context.layout_inflater_service); Layoutinflater Inflater = layoutinflater.from (context);   

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:

1 Public Phonewindow (context context) {  2 3         super (context);  4 5         Mlayoutinflater = layoutinflater.from (context);  6 7}  

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

Layoutinflater.from (context):

    • Public Static Layoutinflater from (context context) {
    • Layoutinflater Layoutinflater =
    • (Layoutinflater) Context.getsystemservice (Context.layout_inflater_service);
    • if (Layoutinflater = = null) {
    • Throw New 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:

1 public view inflate (int. resource, ViewGroup root)  2 3 public view inflate (Xmlpullparser parser, ViewGroup root) 
   4 5 public view inflate (Xmlpullparser parser, ViewGroup root, Boolean attachtoroot)  6 7 public view inflate (int re Source, ViewGroup Root, Boolean attachtoroot)  

Schematic code:

1 Layoutinflater inflater = (layoutinflater) getsystemservice (layout_inflater_service);  2 View view = Inflater.inflate (R.layout.custom, (ViewGroup) Findviewbyid (r.id.test));  3 4//edittext EditText = (EditText) Findviewbyid (r.id.content);//Error  5 6 EditText EditText = (EditText) view.findvi Ewbyid (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.

Acquisition and use of 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.