Layoutinflater analysis of the principle of "reprint"

Source: Internet
Author: User

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

Summary:

believe that contact with Android for a long time friends for Layoutinflater must not be unfamiliar, will know that it is mainly used to load the layout. Friends who have just come into contact with Android may not be familiar with Layoutinflater, because the task of loading the layout is usually done by calling the Setcontentview () method in the activity. In fact Setcontentview () method of the interior is also used Layoutinflater to load the layout, but this part of the source code is internal, it is not easy to see. So today we're going to take Layoutinflater's workflow carefully and perhaps solve some of the doubts that have plagued your mind for years.

First of all, take a look at the basic usage of layoutinflater, it is very simple to use, first need to get to the Layoutinflater instance, there are two methods can be obtained, the first one is as follows:

[Java]View Plaincopy
    1. Layoutinflater Layoutinflater = layoutinflater.from (context);

Of course, there is another way to do the same thing:

[Java]View Plaincopy
    1. Layoutinflater Layoutinflater = (layoutinflater) context
    2. . Getsystemservice (Context.layout_inflater_service);

In fact, the first is the second simple way of writing, just Android gave us a package. Once you have an instance of Layoutinflater, you can call its inflate () method to load the layout, as shown here:

[Java]View Plaincopy
    1. Layoutinflater.inflate (resourceId, Root);

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.

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:

[HTML]View Plaincopy
    1. <linearlayout < span class= "attribute" >xmlns:android= "http://schemas.android.com/apk/res/android"   
    2.     android:id=< Span class= "Attribute-value" > "@+id/main_layout" &NBSP;&NBSP;
    3.      android:layout_width= "match_parent"    
    4.     android:layout_height= "match_parent"  >  
    5. &NBSP;&NBSP;
    6. </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:

[HTML]View Plaincopy
    1. <button xmlns:android= "http://schemas.android.com/apk/res/android"   
    2.     android: Layout_width= "wrap_content" &NBSP;&NBSP;
    3.      android:layout_height= "wrap_content"   
    4.     android:text=" button " >  
    5.   
    6. </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:

[Java]View Plaincopy
  1. Public class Mainactivity extends Activity {
  2. private LinearLayout mainlayout;
  3. @Override
  4. protected void OnCreate (Bundle savedinstancestate) {
  5. super.oncreate (savedinstancestate);
  6. Setcontentview (R.layout.activity_main);
  7. Mainlayout = (linearlayout) Findviewbyid (r.id.main_layout);
  8. Layoutinflater Layoutinflater = Layoutinflater.from (this);
  9. View buttonlayout = layoutinflater.inflate (r.layout.button_layout, null);
  10. Mainlayout.addview (buttonlayout);
  11. }
  12. }

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.

Layoutinflater analysis of the principle of "reprint"

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.