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?
- <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android: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 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?
- View bookmarks_container_2 = Findviewbyid (r.id.bookmarks_favourite);
- 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?
- <merge xmlns:android="http://schemas.android.com/apk/res/android">
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/add"/>
- <Button
- android: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. 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?
- <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:
[Java]View Plaincopyprint?
- ((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 <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