Android Data Filler Layoutinflater

Source: Internet
Author: User

Layoutinflater class in the application is more practical, can be called layout filler, can also become a pump, meaning is to fill the layout files to their desired location, Layoutinflater is used to find the res/layout/under the XML layout file, and instantiating the XML file becomes a view, somewhat similar to Findviewbyid (), but Findviewbyid () is the specific widget control (Button, TextView) found under the XML layout file. for an interface that is not loaded or wants to be loaded dynamically, it needs to be loaded using layoutinflater.inflate () , and for an already loaded interface, use Activiyt.findviewbyid () method to obtain the interface elements .

Layoutinflater Instance Mode

Let's take a look at three simple examples:

Layoutinflater layoutinflater = Getlayoutinflater (); Layoutinflater LayoutInflater2 = Layoutinflater.from (this); Layoutinflater LayoutInflater3 = (layoutinflater) this.getsystemservice (Context.layout_inflater_service);

The first method of Getlayoutinflater :

   Public Layoutinflater Getlayoutinflater () {        return GetWindow (). Getlayoutinflater ();    

The second way of implementation:

    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;    }

The latter two kinds of calls are Getsystemservice, the first kind is also ~

Layoutinflater Implementation Demo

There are four ways to fill the layout layoutinflater, and the demo uses the first type:

Resource: The ID of the layout file needs to be loaded, and the layout file needs to be loaded into the activity to operate.

Root:inflate () Returns a View object if root is not NULL, return this root as the root object, as the root view of the XML file, and if NULL, then the XML file itself is a root view:

public view inflate (int. resource, ViewGroup root) public  view Inflate (Xmlpullparser parser, ViewGroup root)    Publ IC View Inflate (Xmlpullparser parser, ViewGroup root, Boolean attachtoroot)    

Look at the results of the implementation:

Define a button in the main form, this code does not post the ~ Direct Write click event Processing:

Define an item file:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:id=" @+id/test "    android:layout_width=" match_parent "    android:layout_height=" Match_ Parent "    android:orientation=" vertical ">    <textview        android:id=" @+id/inflate_txt "        android: Layout_width= "Match_parent"        android:layout_height= "match_parent"         android:text= "test"/></ Linearlayout>

  Click to handle the event:

Layoutinflater layoutinflater = Getlayoutinflater (); View view = Layoutinflater.inflate (R.layout.item, NULL); TextView Text = (TextView) View.findviewbyid (r.id.inflate_txt); Text.settext ("Http://www.cnblogs.com/xiaofeixiang") ; Text.settextsize (); Text.settextcolor (color.red); Alertdialog.builder Builder = new Alertdialog.builder (mainactivity.this); Builder.setview (view); Alertdialog  alertdialog = Builder.create (); Alertdialog.show ();

Android Data Filler Layoutinflater

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.