Android Abstract Layout--include, merge, viewstub

Source: Internet
Author: User

In layout optimization, Androi's official mentions these three layouts < include/>, < merge/>, < viewstub/>, and describes the advantages of each of these three layouts, The following is also a brief talk about their advantages, and how to use, write down the right as a note.

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" > <inc Lude 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 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 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):

View bookmarks_container_2 = findViewById(R.id.bookmarks_favourite);   bookmarks_container_2.findViewById(R.id.bookmarks_list);
2. Reduce view level < merge/>
< merge/>标签在UI的结构优化中起着非常重要的作用,它可以删减多余的层级,优化UI。< merge/>多用于替换FrameLayout或者当一个布局包含另一个时,< merge/>标签消除视图层次结构中多余的视图组。例如你的主布局文件是垂直布局,引入了一个垂直布局的include,这是如果include布局使用的LinearLayout就没意义了,使用的话反而减慢你的UI表现。这时可以使用< merge/>标签优化。
<Mergexmlns: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 required
< ViewStub />标签最大的优点是当你需要时才会加载,使用他并不会影响UI初始化时的性能。各种不常用的布局想进度条、显示错误消息等可以使用< ViewStub />标签,以减少内存使用量,加快渲染速度。< ViewStub />是一个不可见的,大小为0的View。< ViewStub />标签使用如下:
<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.

This article is reproduced from Zhang Xingye's blog

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.