布局文件的优化
来源:互联网 发布:网络监控结构图 编辑:程序博客网 时间:2024/05/21 19:36
<include/>
<include>标签可以允许在一个布局当中引入另外一个布局,那么比如说我们程序的所有界面都有一个公共的部分,
这个时候最好的做法就是将这个公共的部分提取到一个独立的布局文件当中,然后在每个界面的布局文件当中来引用这个公共的布局。
<include>标签当中可以指定一个layout属性,我们在这个layout属性中填写需要引入的布局名就可以了。而且使用这种引入的方式,
以后如果titlebar的界面有所变更,我们只需要修改titlebar.xml这一个文件就可以了,而不是所有界面一个个地去修改。
在<include>标签当中,我们是可以覆写所有layout属性的,即include中指定的layout属性将会覆盖掉titlebar中指定的layout属性。
想要在<include>标签当中覆写layout属性,必须要将layout_width和layout_height这两个属性也进行覆写,否则覆写效果将不会生效。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/ok"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:text="OK" />
<Button
android:id="@+id/cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:text="Cancel" />
</LinearLayout>
<merge/>
<merge/>标签是作为<include/>标签的一种辅助扩展来使用的,它的主要作用是为了防止在引用布局文件时产生多余的布局嵌套。
<include>标签的用法时主要介绍了它优点,但是它也存在着一个不好的地方,就是可能会导致产生多余的布局嵌套。
在最外层的LinearLayout当中包含了两个元素,一个是EditText,和一个<includ>引入的LinearLayout,这又是一个LinearLayout,
然后在这个内部的LinearLayout当中才包含了确定和取消这两个按钮。这样的话我们就重复的嵌套了一个LinearLayout布局,这时我们应该将
<includ>引入的LinearLayout中的<LinearLayout>标签更换为<merge>,就会避免布局的重复嵌套。
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="@+id/ok"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:text="OK" />
<Button
android:id="@+id/cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:text="Cancel" />
</merge>
仅在需要时才加载布局
大多数人的第一反应就是将不常用的元素使用INVISIBLE或者GONE进行隐藏,然后当用户需要使用这些元素的时候再把它们置成VISIBLE显示出来。
使用这种方式肯定可以实现功能的,但是性能方面就表现得一般了,因为布局还是会加载这些布局文件的。而ViewStub没有大小,没有绘制功能,
也不参与布局,资源消耗非常低,将它放置在布局当中基本可以认为是完全不会影响性能的。
引用的时候使用<ViewStub/>引用布局,它必须要有唯一的Id,让它显示时先获取它的对象:
ViewStub viewStub = (ViewStub) findViewById(R.id.view_stub);
if (viewStub != null) {
View inflatedView = viewStub.inflate();
editExtra1 = (EditText) inflatedView.findViewById(R.id.edit_extra1); //获取它布局中的控件
editExtra2 = (EditText) inflatedView.findViewById(R.id.edit_extra2);
editExtra3 = (EditText) inflatedView.findViewById(R.id.edit_extra3);
}
或者使用viewStub.setVisibility(View.VISIBLE)都可以将隐藏的布局给加载出来
<include>标签可以允许在一个布局当中引入另外一个布局,那么比如说我们程序的所有界面都有一个公共的部分,
这个时候最好的做法就是将这个公共的部分提取到一个独立的布局文件当中,然后在每个界面的布局文件当中来引用这个公共的布局。
<include>标签当中可以指定一个layout属性,我们在这个layout属性中填写需要引入的布局名就可以了。而且使用这种引入的方式,
以后如果titlebar的界面有所变更,我们只需要修改titlebar.xml这一个文件就可以了,而不是所有界面一个个地去修改。
在<include>标签当中,我们是可以覆写所有layout属性的,即include中指定的layout属性将会覆盖掉titlebar中指定的layout属性。
想要在<include>标签当中覆写layout属性,必须要将layout_width和layout_height这两个属性也进行覆写,否则覆写效果将不会生效。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/ok"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:text="OK" />
<Button
android:id="@+id/cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:text="Cancel" />
</LinearLayout>
<merge/>
<merge/>标签是作为<include/>标签的一种辅助扩展来使用的,它的主要作用是为了防止在引用布局文件时产生多余的布局嵌套。
<include>标签的用法时主要介绍了它优点,但是它也存在着一个不好的地方,就是可能会导致产生多余的布局嵌套。
在最外层的LinearLayout当中包含了两个元素,一个是EditText,和一个<includ>引入的LinearLayout,这又是一个LinearLayout,
然后在这个内部的LinearLayout当中才包含了确定和取消这两个按钮。这样的话我们就重复的嵌套了一个LinearLayout布局,这时我们应该将
<includ>引入的LinearLayout中的<LinearLayout>标签更换为<merge>,就会避免布局的重复嵌套。
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="@+id/ok"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:text="OK" />
<Button
android:id="@+id/cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:text="Cancel" />
</merge>
仅在需要时才加载布局
大多数人的第一反应就是将不常用的元素使用INVISIBLE或者GONE进行隐藏,然后当用户需要使用这些元素的时候再把它们置成VISIBLE显示出来。
使用这种方式肯定可以实现功能的,但是性能方面就表现得一般了,因为布局还是会加载这些布局文件的。而ViewStub没有大小,没有绘制功能,
也不参与布局,资源消耗非常低,将它放置在布局当中基本可以认为是完全不会影响性能的。
引用的时候使用<ViewStub/>引用布局,它必须要有唯一的Id,让它显示时先获取它的对象:
ViewStub viewStub = (ViewStub) findViewById(R.id.view_stub);
if (viewStub != null) {
View inflatedView = viewStub.inflate();
editExtra1 = (EditText) inflatedView.findViewById(R.id.edit_extra1); //获取它布局中的控件
editExtra2 = (EditText) inflatedView.findViewById(R.id.edit_extra2);
editExtra3 = (EditText) inflatedView.findViewById(R.id.edit_extra3);
}
或者使用viewStub.setVisibility(View.VISIBLE)都可以将隐藏的布局给加载出来
0 0
- 布局文件的优化
- 提高显示布局文件的性能 1 - 优化布局层级
- Android XML布局文件优化
- 性能优化之布局文件优化
- 布局的优化
- Android布局的优化
- 布局的优化
- UI的布局优化
- android的布局优化
- android布局的优化
- Android布局的优化
- 布局的优化
- 多布局的优化
- Android Training - 提升布局文件的性能(Lesson 1 - 优化布局的层级)
- 利用Hierarchy Viewer优化布局 ---》android布局文件优化
- 【Android Training - Performance】提高显示布局文件的性能[Lesson 1 - 优化布局层级]
- android优化之布局 的优化(二)
- 【Android性能优化】布局的性能优化
- 代码块高级
- 加载properties文件
- 那些程序员出身的畅销书作家
- phpstorm配置代码自动同步到服务器
- Unity3D下 仙剑奇侠传三 人物移动及视角控制的尝试实现及补充
- 布局文件的优化
- UIScrollView循环滚动(三张图片循环滚动)
- 新下拉刷新控件:SwipeRefreshLayout
- 当前时间推后两个工作日的时间
- 已迷失在Python的世界里,如此简单,灵活,强大,优美
- Android - TextView 实现部分文本点击事件
- netbeans 8.0.2 卡在 refresh indices 或者 background scanning of projects
- NSRunLoop Demo
- 关于compileSdkVersion和targetSdkVersion