About Layoutinflater in Android development

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 theLayoutinflater: 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);
Layoutinflater Inflater = Layoutinflater.from (this);    View view=inflater.inflate (r.layout.id, null);     or written     as: View View=layoutinflater.from (this). Inflate (r.layout.id, NULL);   

Method 2:

obtained by the service:

Java code
1 layoutinflater inflater = Layoutinflater.from (this23null  45//67 View View=layoutinflater.from (  thisnull

There are servers to get

1 layoutinflater inflater =2

 

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 will only form an object that is implemented as a view class, and when needed, it is displayed with Setcontentview (view), which is typically displayed through Setcontentview () in the activity. However, 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_ The parent "android:layout_height=" wrap_content "android:id=" @+id/button "android:text=" button/> Dynamically loads the above layout 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);//Here The view is the view object obtained aboveTextView = (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 ().
Findviewbyid has two forms r.layout.xx is a layout file referencing Res/layout/xx.xml (Inflate method), r.id.xx is a component referencing 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.

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.