Three layouts <include/>, <merge/>, <viewstub/>

Source: Internet
Author: User

1. Layout Reuse <include/>

<include/> Tags can reuse layout files, very simple to use.

<layout= "@layout/layoutname"/>

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 defines an ID, 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/>

<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.

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:

< 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 <merge/> tags.


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

Three layouts <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.