Android Abstract Layout--include, merge, viewstub

Source: Internet
Author: User

Android Abstract Layout--include, merge, Viewstub
In layout optimization, Android officially mentions the three layouts of include, merge, Viewstub and describes the advantages of each of these three layouts, and here's a brief look at their strengths and how to use them.
1. Layout reuse include
The include tag allows you to reuse layout files, which are simple to use:

<linearlayout xmlns:android= "http:/ /schemas.android.com/apk/res/android " android:orientation=" vertical " an Droid:layout_width= "Match_parent" android:layout_height= "match_parent" Android:background=" @color/app_bg " android:gravity=" center_horizontal " > <include                Layout= "@layout/titlebar" /> <textview android:layout_width= "Match_parent" Android:layout_height= "wrap_content"  android:text= "@string/hello"  android:padding= "10DP" /> 
    
      ...  </LinearLayout> 
     

1) The include tag can use a separate layout property, which must also be used.
2) Other properties can be used. If the include tag specifies an id attribute and your layout defines the ID, then your layout ID will be overwritten.
3) All android:layout_* in the include tag are valid, provided that you have to write Layout_width and layout_height two properties.
4) The layout can contain two identical include tags, which can be resolved using the following methods (reference):

View bookmarks_container_2 = findViewById(R.id.bookmarks_favourite);   bookmarks_container_2.findViewById(R.id.bookmarks_list)

2. Reduce the View-level merge
The merge tag plays a very important role in the structure optimization of the UI, and it can prune unnecessary layers and optimize the UI. Merge is used to replace framelayout or when a layout contains another layout, the Merge tab eliminates the extra view groups in the view hierarchy.
Eg: your main layout file is a vertical layout that introduces a vertical layout of include, which is meaningless if the linearlayout used by the include layout makes sense, and instead slows down your UI performance. You can use the Merge tab to optimize this.

<merge xmlns:android="Http://schemas.android.com/apk/res/android">      <buttonandroid:layout_width="Fill_parent"android:layout_height=  "Wrap_content"android:text="@string/add"/>                                     <buttonandroid:layout_width="Fill_parent"android:layout_height=  "Wrap_content"android:text="@string/delete"/>                                 </Merge>  

Now, when you add the layout file (using the Include tag), the system ignores the merge node and adds two buttons directly.
3. Use viewstub when needed

The biggest advantage of the viewstub tag is that it does not load when you need it, and it doesn't affect performance when the UI is initialized. A variety of infrequently used layouts like progress bars, display error messages, and so on can use viewstub tags to reduce memory usage and speed rendering. The viewstub tag is an invisible, 0-size view. Use the following:

<ViewStub      android:id="@+id/stub_import"      android:inflatedId="@+id/panel_import"      android:layout="@layout/progress_overlay"      android:layout_width="fill_parent"      android:layout_height="wrap_content"      android:layout_gravity="bottom" />  

When you want to load a layout, you can use one of the following methods:

((ViewStub) findViewById(R.id.stub_import)).setVisibility(View.VISIBLE);  or  View importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate();  

When the inflate () function is called, viewstub is substituted for the referenced resource and returns the referenced view. This way the program can directly get the referenced view without calling the function Findviewbyid () again to find it.
Note: Viewstub currently has a flaw that does not support the above merge tag.

Android Abstract Layout--include, merge, viewstub

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.