Android include, merge, ViewStub

作者: IOwl | 来源:发表于2017-03-17 15:28 被阅读315次

参考:http://blog.csdn.net/xyz_lmn/article/details/14524567

布局优化的时候经常用到的三种方式

  • include
  • merge
  • ViewStub

1、<include />标签重用布局

include标签能够重用已定义好的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:gravity="center"
              android:orientation="vertical">

    <TextView
        android:id="@+id/id_text_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <include
        android:id="@+id/id_text_2"
        layout="@layout/layout_1"
        android:layout_width="match_parent"
        android:layout_height="0"
        android:layout_weight="0.5"/>

</LinearLayout>
重用的布局layout_1
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/id_reuse_layout"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:gravity="center"
              android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <include layout="@layout/嵌套的其他重用布局"/>

</LinearLayout>
1)必须声明一个layout属性,值为重用的布局
2)可以声明其他属性,include中声明的id会覆盖重用布局中的id

eg:上面的代码中id_text_2会覆盖id_reuse_layout

3)可以使用全部的android:layout_*属性,必须声明android:layout_width和android:layout_height属性

eg:上面的布局中weight生效的前提是width和height属性必须声明

4)可以嵌套使用

2、<merge />标签减少视图层级

<merge/>标签在UI的结构优化中起着非常重要的作用,它可以删减多余的层级,优化UI。<merge/>多用于替换当一个布局包含另一个布局时,<merge/>标签消除视图层次结构中多余的层级。例如你的主布局文件是垂直布局,引入了一个垂直布局的include,这是如果include布局使用的LinearLayout就没意义了,使用的话反而减慢你的UI表现。这时可以使用<merge/>标签优化。
<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> 
现在添加该布局时(使用<include />标签),系统忽略<merge />层并且直接添加两个Button到你的布局中。

3、<ViewStub />标签占位

<ViewStub />标签最大的优点是当你需要时才会加载,使用他并不会影响UI初始化时的性能。各种不常用的布局想进度条、显示错误消息等可以使用<ViewStub />标签,以减少内存使用量,加快渲染速度。<ViewStub />是一个不可见的,大小为0的View。
<ViewStub  
        android:id="@+id/stub_import"  
        android:layout="@layout/progress_overlay"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content" />  
当你想加载布局时,可以使用下面其中一种方法:
((ViewStub) findViewById(R.id.stub_import)).setVisibility(View.VISIBLE);  
// or  
View importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate();
当调用inflate()函数的时候,ViewStub被引用的资源替代,并且返回引用的view。 这样程序可以直接得到引用的view而不用再次调用函数findViewById()来查找了。

注:ViewStub目前有个缺陷就是还不支持 <merge /> 标签。

相关文章

网友评论

    本文标题:Android include, merge, ViewStub

    本文链接:https://www.haomeiwen.com/subject/tvuenttx.html