博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android抽象布局——include、merge 、ViewStub
阅读量:4115 次
发布时间:2019-05-25

本文共 3099 字,大约阅读时间需要 10 分钟。

  在布局优化中,Androi的官方提到了这三种布局<include />、<merge />、<ViewStub />,并介绍了这三种布局各有的优势,下面也是简单说一下他们的优势,以及怎么使用,记下来权当做笔记。

1、布局重用<include />

<include />标签能够重用布局文件,简单的使用如下:

[html]
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:orientation="vertical"
  3. android:layout_width=”match_parent”
  4. android:layout_height=”match_parent”
  5. android:background="@color/app_bg"
  6. android:gravity="center_horizontal">
  7. <include layout="@layout/titlebar"/>
  8. <TextView android:layout_width=”match_parent”
  9. android:layout_height="wrap_content"
  10. android:text="@string/hello"
  11. android:padding="10dp" />
  12. ...
  13. </LinearLayout>
...

1)<include />标签可以使用单独的layout属性,这个也是必须使用的。

2)可以使用其他属性。<include />标签若指定了ID属性,而你的layout也定义了ID,则你的layoutID会被覆盖,。

3)在include标签中所有的android:layout_*都是有效的,前提是必须要写layout_widthlayout_height两个属性

4)布局中可以包含两个相同的include标签,引用时可以使用如下方法解决():

[html]
  1. View bookmarks_container_2 = findViewById(R.id.bookmarks_favourite);
  2. bookmarks_container_2.findViewById(R.id.bookmarks_list);
View bookmarks_container_2 = findViewById(R.id.bookmarks_favourite); bookmarks_container_2.findViewById(R.id.bookmarks_list);

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

<merge/>标签在UI的结构优化中起着非常重要的作用,它可以删减多余的层级,优化UI。<merge/>多用于替换FrameLayout或者当一个布局包含另一个时,<merge/>标签消除视图层次结构中多余的视图组。例如你的主布局文件是垂直布局,引入了一个垂直布局的include,这是如果include布局使用的LinearLayout就没意义了,使用的话反而减慢你的UI表现。这时可以使用<merge/>标签优化。

[html]
  1. <merge xmlns:android="http://schemas.android.com/apk/res/android">
  2. <Button
  3. android:layout_width="fill_parent"
  4. android:layout_height="wrap_content"
  5. android:text="@string/add"/>
  6. <Button
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:text="@string/delete"/>
  10. </merge>

现在,当你添加该布局文件时(使用<include />标签),系统忽略<merge />节点并且直接添加两个Button。更多<merge />介绍可以参考

3、需要时使用<ViewStub />

<ViewStub />标签最大的优点是当你需要时才会加载,使用他并不会影响UI初始化时的性能。各种不常用的布局想进度条、显示错误消息等可以使用<ViewStub />标签,以减少内存使用量,加快渲染速度。<ViewStub />是一个不可见的,大小为0View。<ViewStub />标签使用如下:

[html]
  1. <ViewStub
  2. android:id="@+id/stub_import"
  3. android:inflatedId="@+id/panel_import"
  4. android:layout="@layout/progress_overlay"
  5. android:layout_width="fill_parent"
  6. android:layout_height="wrap_content"
  7. android:layout_gravity="bottom" />

当你想加载布局时,可以使用下面其中一种方法:

[java]
  1. ((ViewStub) findViewById(R.id.stub_import)).setVisibility(View.VISIBLE);
  2. // or
  3. View importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate();
((ViewStub) findViewById(R.id.stub_import)).setVisibility(View.VISIBLE);// orView importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate();

当调用inflate()函数的时候,ViewStub被引用的资源替代,并且返回引用的view 这样程序可以直接得到引用的view而不用再次调用函数findViewById()来查找了。

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

更多<ViewStub />标签介绍可以参考《》

/**
* @author 张兴业
*
* 我的新浪微博:
*/

参考:

http://developer.android.com/training/improving-layouts/reusing-layouts.html

http://android-developers.blogspot.com/2009/03/android-layout-tricks-3-optimize-by.html

http://developer.android.com/training/improving-layouts/optimizing-layout.html#Lint

http://android-developers.blogspot.com/2009/02/android-layout-tricks-1.html

http://developer.android.com/training/improving-layouts/loading-ondemand.html

转载地址:http://uezpi.baihongyu.com/

你可能感兴趣的文章
C#中ColorDialog需点两次确定才会退出的问题
查看>>
数据库
查看>>
nginx反代 499 502 bad gateway 和timeout
查看>>
linux虚拟机安装tar.gz版jdk步骤详解
查看>>
python猜拳游戏
查看>>
python实现100以内自然数之和,偶数之和
查看>>
python数字逆序输出及多个print输出在同一行
查看>>
ESP8266 WIFI数传 Pixhaw折腾笔记
查看>>
苏宁产品经理面经
查看>>
百度产品经理群面
查看>>
去哪儿一面+平安科技二面+hr面+贝贝一面+二面产品面经
查看>>
element ui 弹窗在IE11中关闭时闪现问题修复
查看>>
vue 遍历对象并动态绑定在下拉列表中
查看>>
Vue动态生成el-checkbox点击无法选中的解决方法
查看>>
python __future__
查看>>
MySQL Tricks1
查看>>
python 变量作用域问题(经典坑)
查看>>
pytorch
查看>>
pytorch(二)
查看>>
pytorch(三)
查看>>