Android Abstract Layout--include, merge, Viewstub detailed _android

Source: Internet
Author: User

In the layout optimization, Androi official mentioned the three kinds of layout <include/>, <merge/>, <viewstub/>, and introduced the advantages of these three layouts, the following is also a brief description of their advantages, and how to use , write down the right to take notes.

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/titlebar"/> 
 
 <textview android:layout_width= "match_parent" 
    android:layout_ height= "Wrap_content" 
    android:text= "@string/hello" 
    android:padding= "10DP"/> 
 
 ... 
 
</LinearLayout> 

1 <include/> Tags can use a separate layout attribute, 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, the solution.

3 All android:layout_* in the include tag are valid, provided that the Layout_width and layout_height two properties must be written.

4 The layout can contain two identical include tags, which can be resolved using the following method (reference):

View bookmarks_container_2 = Findviewbyid (r.id.bookmarks_favourite); 
 

2, reduce the view level <merge/>

<merge/> tags play a very important role in the structure optimization of the UI, which can be used to prune redundant levels and optimize the UI. <merge/> used to replace framelayout or when one layout contains another,,<merge/> tags eliminate redundant view groups in the view hierarchy. For example, your main layout file is a vertical layout, with the introduction of a vertical layout include, which is meaningless if the linearlayout is used by the include layout, which slows down your UI performance. At this point you can use <merge/> label optimization.

 <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 button 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 is loaded when you need it, and using it does not affect the performance of UI initialization. A variety of unusual layouts like progress bars, display error messages, etc. you can use the <viewstub/> tag to reduce memory usage and speed up rendering. <viewstub/> is a view that is not visible, size 0. <viewstub/> Tags are used as follows:

<viewstub 
 android:id= "@+id/stub_import" 
 android:inflatedid= "@+id/panel_import" 
 @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 

When the inflate () function is invoked, Viewstub is substituted for the referenced resource and returns the referenced view. This allows the program to get the referenced view directly without calling the function Findviewbyid () again to find it.

Note: Viewstub currently has a flaw that does not support <merge/> tags.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.