Android Abstract Layout--include, merge, viewstub

Source: Internet
Author: User

in the layout optimization, Androi of the official mention of the three layout <include/>, <merge/>, <viewstub/>, and introduced the advantages of each of the three layouts, the following is a brief talk about their advantages, And how to use it, write down the right to take notes.

1. Layout Reuse <include/>

<include/> Tags can reuse layout files, simple to use as follows:

[HTML]View Plaincopyprint?
  1. <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:orientation="vertical"
  3. android:layout_width= "Match_parent"
  4. android:layout_height= "Match_parent"
  5. android:background="@color/app_bg"
  6. android:gravity="Center_horizontal">
  7. <include layout="@layout/titlebar"/>
  8. <TextView android:layout_width= "match_parent"
  9. android:layout_height="wrap_content"
  10. android:text="@string/hello"
  11. android:padding="10DP" />
  12. ...
  13. </linearlayout>

1) The <include/> Tag can use a separate layout property, which is also required.

2) Other properties can be used. <include/> Tag If you specify an id attribute, and your layout also defines an ID, your layout ID will be overwritten with the solution.

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):

[HTML]View Plaincopyprint?
    1. View bookmarks_container_2 = Findviewbyid (r.id.bookmarks_favourite);
    2. Bookmarks_container_2.findviewbyid (r.id.bookmarks_list);


2. Reduce the view level <merge/>

<merge/> tags play a very important role in the structure optimization of the UI, which can prune unnecessary layers and optimize the UI. <merge/> More is used to replace framelayout or when one layout contains another,<merge/> the label eliminates the extra view group in the view hierarchy. For example, 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 <merge/> tag optimization at this point.

[HTML]View Plaincopyprint?
  1. <merge xmlns:android="http://schemas.android.com/apk/res/android">
  2. <Button
  3. android:layout_width="fill_parent"
  4. android:layout_height="wrap_content"
  5. android:text="@string/add"/>
  6. <Button
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:text="@string/delete"/>
  10. </Merge>

Now, when you add the layout file (using the <include/> tag), the system ignores the <merge/> node and adds two buttons directly. More <merge/> Introduction can refer to "Android Layout Tricks #3: Optimize by merging"

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. <viewstub/> is an invisible, 0-size View. <viewstub/> Tags are used as follows:

[HTML]View Plaincopyprint?
  1. <viewstub
  2. android:id="@+id/stub_import"
  3. android:inflatedid="@+id/panel_import"
  4. android:layout="@layout/progress_overlay"
  5. android:layout_width="fill_parent"
  6. android:layout_height="wrap_content"
  7. android:layout_gravity="Bottom" />

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

[Java]View Plaincopyprint?
    1. ((viewstub) Findviewbyid (R.id.stub_import)). Setvisibility (view.visible);
    2. Or
    3. 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 <merge/> tags.


More <viewstub/> Label Introduction can refer to "Android Layout Tricks #3: Optimize with stubs"

/*** @author Zhang Xingye * http://blog.csdn.net/xyz_lmn* my Sina Weibo: @ Zhang Xingye tbow*/

Reference:

Http://developer.android.com/training/improving-layouts/reusing-layouts.html

Http://android-developers.blogspot.com/2009/03/android-layout-tricks-3-optimize-by.html

Http://developer.android.com/training/improving-layouts/optimizing-layout.html#Lint

Http://android-developers.blogspot.com/2009/02/android-layout-tricks-1.html

Http://developer.android.com/training/improving-layouts/loading-ondemand.html

    • Previous Android UI Development 39th Chapter--tab Interface Implementation Summary and comparison
    • Next Android Touch and gesture Operation Gesturedetector

Android Abstract Layout--include, merge, viewstub

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.