Use of Layoutinflater in Android

Source: Internet
Author: User

Inflater English means expansion, in Android should be extended meaning it.
The Layoutinflater function is similar to Findviewbyid (), and the difference is that Layoutinflater is used to find the XML layout file under the Layout folder and instantiate it! Findviewbyid () is to find specific widget controls (such as Button,textview, etc.) under a specific XML.

There are 3 ways to get its usage:

Method 1:

By the static function of the Layoutinflater: from(context context) gets:

Static Layoutinflater from (context context);

Such as:

Java code
    1. Layoutinflater Inflater = Layoutinflater.from (this);
    2. View view=inflater.inflate (r.layout.id, null);
    3. or written as:
    4. View View=layoutinflater.from (This). Inflate (r.layout.id, null);
Java code
    1. Layoutinflater Inflater = Layoutinflater.from (this);
    2. View view=inflater.inflate (r.layout.id, null);
    3. or written as:
    4. View View=layoutinflater.from (this). Inflate (r.layout.id, null);

Method 2:

obtained by the service:

Java code
    1. Layoutinflater Inflater = (layoutinflater) context.getsystemservice
    2. (Context.layout_inflater_service);
Java code
    1. Layoutinflater Inflater = (layoutinflater) context.getsystemservice
    2. (Context.layout_inflater_service);

Method 3:

Call the activity's getlayoutinflater() function to get the layoutinflater object.

Setcontentview and Inflate differences

Ext.:http://blog.163.com/promise_wg/blog/static/18912001420116241062211/

Generally do one thing with Layoutinflater: inflate

Inflate this method has four different forms (see below), the purpose is to transform the layout of the XML expression into a View object.
One of the more commonly used, View inflate (int resource, ViewGroup root), the other three, in fact, the purpose is almost.
int resource, which is the ID of the Resource/layout file in the R file, this must be specified.
and ViewGroup root can be null,null when only create a resource corresponding view, not NULL, the created view will be automatically added to the root child.

Setcontentview () Once called, layout will immediately display the UI;Inflate only creates an object that is implemented as a view class, and then displays it with Setcontentview (view) when necessary.
The interface is typically displayed in activity through Setcontentview (), but if the control layout is set to operate in non-activity, this requires layoutinflater dynamic loading
< TextView
Android:id= "@+id/tview"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "Ataaw.com"
/>
< Button
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Android:id= "@+id/button"
android:text= "button"
/>
Load the above layout dynamically in the program.
Layoutinflater Flater =Layoutinflater.from (this);
View view = Flater. Inflate (r.layout.example, NULL);
Gets the controls in the layout.
Button = (button) View.findviewbyid (R.id.button);// This view is the view object obtained above
TextView = (TextView) View.findviewbyid (R.id.tview);

Layoutinflater.inflate () Converts the layout file to view, which is intended for use by layout inflater. Although layout is also a subclass of view, in Android, if you want to convert the layout of XML into view into a. Java code, you can only pass inflater, not through Findviewbyid ().

There are two forms of Findviewbyid
R.LAYOUT.XX is a layout file that references Res/layout/xx.xml (inflate method), r.id.xx is the component that references the layout file, and the component ID is xx (Findviewbyid method). All component IDs can be viewed with r.id.xx, but the components are not available in Layout setcontentview (), and Activity.findviewbyid () will have null pointer exceptions
A. Findviewbyid (int id) in activity
B. Findviewbyid (int id) in View
The difference is that Layoutinflater is used to find layouts under the XML layout file and instantiate it! Instead, Findviewbyid () is looking for specific widget controls (such as: Button,textview, etc.) under specific XML.

Use of Layoutinflater in Android

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.