"Doing the project while learning Android" Knowledge Point: Dynamic set Layout Layoutinflater

Source: Internet
Author: User

I. Role:

The Layoutinflater function is to instantiate an XML layout file of layout as a view class object, and Layoutinflater acts like Findviewbyid (). 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.

Two. Three ways to get Layoutinflater instances

1.LayoutInflater inflater = Getlayoutinflater (); Call activity's Getlayoutinflater ()

2.LayoutInflater Inflater = Layoutinflater.from (this);

3.LayoutInflater Inflater = (layoutinflater) context.getsystemservice (Layout_inflater_service);

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:

Public Phonewindow (Context context) {          super (context);          Mlayoutinflater = Layoutinflater.from (context);  }  

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 ().

Three. After instantiating the Layoutinflater, instantiate the layout XML file as a view class object.

1.

Layoutinflater inflater = Getlayoutinflater (); View view=inflater.inflate (r.layout.id, NULL);    

2.

Layoutinflater Inflater = Layoutinflater.from (this);  View view=inflater.inflate (r.layout.id, NULL);

3.

Layoutinflater Inflater = (layoutinflater) context.getsystemservice (Layout_inflater_service); View view=inflater.inflate (r.layout.id, NULL);
Four. 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:

    • Public View Inflate (int resource, ViewGroup root) (common)
      • The inflate () method generally receives two parameters, the first parameter is the layout ID to be loaded, and the second parameter refers to the outside of the layout and then nested a layer of parent layout, if you do not need to directly pass NULL. This succeeds in successfully creating an instance of the layout and then adding it to the specified location to be displayed.
    • 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)

Schematic code:

<pre name= "code" class= "java" >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 above code, the second parameter ViewGroup root is specified, and of course you can set the null value.

Instance:

Let's look at the usage of layoutinflater more intuitively in a very simple small example. For example, there is currently a project, where mainactivity corresponding layout file is called Activity_main.xml, the code is as follows:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    android:id= "@+id/main_layout"    android:layout_width= "match_parent"    android:layout_height= "Match_parent" ></LinearLayout>
The content of this layout file is very simple, there is only an empty linearlayout, there are no controls in it, so nothing should be displayed on the interface.

So then we'll define a layout file, name it button_layout.xml, and the code looks like this:

<button xmlns:android= "http://schemas.android.com/apk/res/android"    android:layout_width= "Wrap_content"    android:layout_height= "wrap_content"    android:text= "button" ></Button>

This layout file is also very simple, with only one button.

Now we have to figure out how to add the Button_layout layout to the linearlayout of the main layout file through Layoutinflater. Modify the code in Mainactivity, as shown below, according to the usage you just introduced:

public class Mainactivity extends Activity {    private linearlayout mainlayout;    @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity_main);        Mainlayout = (linearlayout) Findviewbyid (r.id.main_layout);        Layoutinflater Layoutinflater = Layoutinflater.from (this);        View buttonlayout = layoutinflater.inflate (r.layout.button_layout, null);        Mainlayout.addview (buttonlayout);    }}

As you can see, this first obtains the instance of Layoutinflater, then calls its inflate () method to load the Button_layout layout, and finally calls LinearLayout's AddView () method to add it to the linearlayout.

You can now run the program with the results as shown:

The Button is displayed on the interface! It is true that we have successfully added the Button_layout layout to linearlayout with the help of Layoutinflater. Layoutinflater technology is widely used in the need to dynamically add view, such as in ScrollView and ListView, often can see layoutinflater figure.

Reference:

Http://www.cnblogs.com/merryjd/archive/2013/01/08/2851092.html

http://blog.csdn.net/guolin_blog/article/details/12921889

"Doing the project while learning Android" Knowledge Point: Dynamic set Layout 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.