Android布局优化之include、merge、ViewStub的使用
本文针对include、merge、ViewStub三个标签如何在布局复用、有效减少布局层级以及如何可以按需加载三个方面进行介绍的。
复用布局可以帮助我们创建一些可以重复使用的复杂布局。这种方式也意味着应用中任何在多个布局文件之间使用的通用布局都可以被提取出来,然后分别进行管理,使用的时候再进行组合。因此当我们在自定义一些View的时候,使用复用布局会更简单方便。在平常开发中使用可以复用的布局文件,不仅仅是因为它可以有效减少布局文件数量,更多的目的在于它更方面我们管理应用,布局复用,在更改某个组件时就可以做到改一个布局文件就更改了应用中所有引用该布局文件的组件,做到一改全改。
<include/>
<include/>标签在布局优化中是使用最多的一个标签了,它就是为了解决重复定义布局的问题。<include/>标签就相当于C、C++中的include头文件一样,把一些常用的底层的API封装起来,需要的时候引入即可。在一些开源的J2EE中许多XML配置文件也都会使用<include/>标签,将多个配置文件组合成为一个更为复杂的配置文件,如最常见的S2SH。
在以前Android开发中,由于ActionBar设计上的不统一以及兼容性问题,所以很多应用都自定义了一套自己的标题栏titlebar。标题栏我们知道在应用的每个界面几乎都会用到,在这里可以作为一个很好的示例来解释<include/>标签的使用。
下面是一个自定义的titlebar文件:
|
1
2
3
4
5
6
7
8
9
|
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/titlebar_bg">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/gafricalogo" />
</FrameLayout>
|
在应用中使用titlebar布局文件,我们通过<include/>标签,布局文件如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<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>
|
在<include/>标签中可以覆盖导入的布局文件root布局的布局属性(如layout_*属性)。
布局示例如下:
|
1
2
3
4
|
<include android:id="@+id/news_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="@layout/title"/>
|
如果想使用<include/>标签覆盖嵌入布局root布局属性,必须同时覆盖layout_height和layout_width属性,否则会直接报编译时语法错误。
Layout parameter layout_height ignored unless layout_width is also specified on <include> tag
如果<include/>标签已经定义了id,而嵌入布局文件的root布局文件也定义了id,<include>标签的id会覆盖掉嵌入布局文件root的id,如果include标签没有定义id则会使用嵌入文件root的id。
<merge/>
<merge/>标签都是与<include/>标签组合使用的,它的作用就是可以有效减少View树的层次来优化布局。
下面通过一个简单的示例探讨一下<merge/>标签的使用,下面是嵌套布局的layout_text.xml文件:
|
1
2
3
4
5
6
7
8
9
10
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:text="Hello World!"
android:layout_height="match_parent" />
</LinearLayout>
|
一个线性布局中嵌套一个文本视图,主布局如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_wrap"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include
android:id="@+id/layout_import"
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="@layout/layout_text" />
</LinearLayout>
|
通过hierarchyviewer我们可以看到主布局View树的部分层级结构如下图:

现在讲嵌套布局跟布局标签更改为<merge/>,merge_text.xml布局文件如下:
|
1
2
3
4
5
6
7
8
9
|
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hello World!"/>
</merge>
|
然后将主布局<include/>标签中的layout更改为merge_text.xml,运行后重新截图如下:

对比截图就可以发现上面的四层结构,现在已经是三层结构了。当我们使用<merge/>标签的时候,系统会自动忽略merge层级,而把TextView直接放置与<include/>平级。
<merge/>标签在使用的时候需要特别注意布局的类型,例如我的<merge/>标签中包含的是一个LinearLayout布局视图,布局中的元素是线性排列的,如果嵌套进主布局时,include标签父布局时FrameLayout,这种方式嵌套肯定会出问题的,merge中元素会按照FrameLayout布局方式显示。所以在使用的时候,<merge/>标签虽然可以减少布局层级,但是它的限制也不可小觑。
<merge/>只能作为XML布局的根标签使用。当Inflate以<merge/>开头的布局文件时,必须指定一个父ViewGroup,并且必须设定attachToRoot为true。
View android.view.LayoutInflater.inflate(int resource, ViewGroup root, boolean attachToRoot)
root不可少,attachToRoot必须为true。
ViewStub
在开发过程中,经常会遇到这样一种情况,有些布局很复杂但是却很少使用。例如条目详情、进度条标识或者未读消息等,这些情况如果在一开始初始化,虽然设置可见性View.GONE,但是在Inflate的时候View仍然会被Inflate,仍然会创建对象,由于这些布局又想到复杂,所以会很消耗系统资源。
ViewStub就是为了解决上面问题的,ViewStub是一个轻量级的View,它一个看不见的,不占布局位置,占用资源非常小的控件。
定义ViewStub布局文件
下面是一个ViewStub布局文件:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_wrap"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ViewStub
android:id="@+id/stub_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inflatedId="@+id/image_import"
android:layout="@layout/layout_image" />
<ViewStub
android:id="@+id/stub_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inflatedId="@+id/text_import"
android:layout="@layout/layout_text" />
</LinearLayout>
|
layout_image.xml文件如下(layout_text.xml类似):
|
1
2
3
4
5
6
7
8
9
10
11
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/layout_image">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
|
加载ViewStub布局文件
动态加载ViewStub所包含的布局文件有两种方式,方式一使用使用inflate()方法,方式二就是使用setVisibility(View.VISIBLE)。
示例java代码如下:
|
1
2
3
4
5
6
7
8
9
10
11
|
private ViewStub viewStub;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main2);
viewStub = (ViewStub) findViewById(R.id.stub_image);
//viewStub.inflate();//方式一
viewStub.setVisibility(View.VISIBLE);//方式二
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageResource(R.drawable.image);
}
|
示例View层级截图如下:

ViewStub一旦visible/inflated,它自己就不在是View试图层级的一部分了。所以后面无法再使用ViewStub来控制布局,填充布局root布局如果有id,则会默认被
android:inflatedId所设置的id取代,如果没有设置android:inflatedId,则会直接使用填充布局id。
由于ViewStub这种使用后即可就置空的策略,所以当需要在运行时不止一次的显示和隐藏某个布局,那么ViewStub是做不到的。这时就只能使用View的可见性来控制了。
layout_*相关属性与include标签相似,如果使用应该在ViewStub上面使用,否则使用在嵌套进来布局root上面无效。
ViewStub的另一个缺点就是目前还不支持merge标签。
小结
Android布局优化基本上就设计上面include、merge、ViewStub三个标签的使用。在平常开发中布局推荐使用RelativeLayout,它也可以有效减少布局层级嵌套。最后了将merge和include源码附上,ViewStub就是一个View,就不贴出来了。
Include源码
|
1
2
3
4
5
6
7
8
9
10
|
/**
* Exercise <include /> tag in XML files.
*/
public class Include extends Activity {
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.include_tag);
}
}
|
Merge源码
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
/**
* Exercise <merge /> tag in XML files.
*/
public class Merge extends Activity {
private LinearLayout mLayout;
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
mLayout = new LinearLayout(this);
mLayout.setOrientation(LinearLayout.VERTICAL);
LayoutInflater.from(this).inflate(R.layout.merge_tag, mLayout);
setContentView(mLayout);
}
public ViewGroup getLayout() {
return mLayout;
}
}
|
Android布局优化之include、merge、ViewStub的使用的更多相关文章
- Android布局优化:include 、merge、ViewStub的详细总结
版权声明:本文出自汪磊的博客,未经作者允许禁止转载. 本篇博客主要是对上篇博客的补充Android性能优化之UI渲染性能优化, 没有什么新东西,觉得应该是都掌握的玩意,写出来也只是自己做个小小的总结. ...
- [Android]Android布局优化之<include />
转载请标明:转载于http://www.cnblogs.com/Liuyt-61/p/6602891.html -------------------------------------------- ...
- 【转】Android布局优化之ViewStub
ViewStub是Android布局优化中一个很不错的标签/控件,直接继承自View.虽然Android开发人员基本上都听说过,但是真正用的可能不多. ViewStub可以理解成一个非常轻量级的Vie ...
- android 布局优化常用技巧
android对多个模块都要是要的UI逻辑的致辞除了fragment之外,没有别的东西可以支持了, include,merge,viewstub只能支持公用的ui,但是这个通用支持不能包含逻辑(jav ...
- 转:Android布局优化
categories: Android 在Android开发中,我们常用的布局方式主要有LinearLayout.RelativeLayout.FrameLayout等,通过这些布局我们可以实现各种各 ...
- [旧][Android] 布局优化
备注 原发表于2016.05.21,资料已过时,仅作备份,谨慎参考 前言 最近在编写布局时,发现这一块是有很多值得深入学习的地方的.毕竟应用开发,界面展示是十分重要的部分.另外在开发时,为自己的代码做 ...
- Android的布局优化之include、merge 、viewstub
以前在写布局的时候总是喜欢用自己熟悉的方式去写,从来也没有想过优化怎么的,后来又一次在上班的时候老大拿着我写的一个页面说我这个不行.我说这不是和设计图上的一模一样的么?怎么就不行了?然后他就跟我说了一 ...
- Android布局优化之ViewStub、include、merge使用与源码分析
在开发中UI布局是我们都会遇到的问题,随着UI越来越多,布局的重复性.复杂度也会随之增长.Android官方给了几个优化的方法,但是网络上的资料基本上都是对官方资料的翻译,这些资料都特别的简单,经常会 ...
- 布局重用 include merge ViewStub
在布局优化中,Androi的官方提到了这三种布局<include />.<merge />.<ViewStub />,并介绍了这三种布局各有的优势,下面也是简单说一 ...
随机推荐
- apache开源项目--mina
Apache MINA(Multipurpose Infrastructure for Network Applications) 是 Apache 组织一个较新的项目,它为开发高性能和高可用性的网络 ...
- poj 2063 Investment
题意:给定一个初始资金capital,然后给定d种投资方案,每种投资方案中有投资额value[i](是1000的倍数)和利息interest[i],每年的投资就可以拿到全部利息,然后累加起来继续投资利 ...
- 【转】Windows环境下Android Studio v1.0安装教程
原文网址:http://ask.android-studio.org/?/article/9 http://android-studio.org/index.php/docs/experience/1 ...
- C# C/S 结构操作Ini系统文件
Winfrom 开发时,有时会将一些系统某个设置保存到Ini 类型的文件中.下面提供操作Ini 文件的代码: public static class IniFiles { [DllImport(&qu ...
- HDU 1247 Hat’s Words
Hat’s Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- Android中不混淆类中函数
情况一:混淆不同的函数aTest.bTest -keep class com.zony.Test { void aTest(byte[], int, int); void bTest(String, ...
- alibaba笔试
1.D A(7,7)/(A(3,3)A(2,2)) = 420 关联: 字符串的排列和组合:http://blog.csdn.net/hackbuteer1/article/details/7462 ...
- cxf 动态创建客户端,局域网能正常调用服务端,外网不能访问
- Annotations:注解
注解,作为元数据的一种形式,虽不是程序的一部分,却有以下作用: 可以让编译器跳过某些检测 某些工具可以根据注解信息生成文档等 某些注解可以在运行时检查 @表示这是一个注解 @Override ...
- eucalyptus,openNebula云构建漫谈
Eucalyptus篇 万事在于理解,技术研究贵在入脑入心.生活很苦,乐趣何在,在于君心?不能修心,则诸事繁杂!闲来无事,阅读官网文档遇此 two Why selecter?因此分享给亲们!亲,那么我 ...