In the layout optimization, Android's official mentions these three kinds of layout <include/>, <merge/>, <viewstub/>, and introduced these three kinds of layout each has the superiority, below is also briefly says how to use.
1. Layout Reuse <include/><include/> Tags can reuse layout files, simple to use as follows:
<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/layout_title "/> <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) If the 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
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.
<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 "/>
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 neededThe 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:
<viewstub android:id= "@+id/stub_import" android:inflatedid= "@+id/panel_stub" 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 <merge/> tags.
More <viewstub/> Label Introduction can refer to "Android Layout Tricks #3: Optimize with stubs"
Welcome to scan QR Code, follow public account
Android Layout Reuse and optimization