Android布局优化之 - 复用、减少层级、按需加载

本文针对include、merge、ViewStub三个标签如何在布局复用、有效减少布局层级以及如何可以按需加载三个方面进行介绍的。
复用布局可以帮助我们创建一些可以重复使用的复杂布局。这种方式也意味着应用中任何在多个布局文件之间使用的通用布局都可以被提取出来,然后分别进行管理,使用的时候再进行组合。因此当我们在自定义一些View的时候,使用复用布局会更简单方便。在平常开发中使用可以复用的布局文件,不仅仅是因为它可以有效减少布局文件数量,更多的目的在于它更方面我们管理应用,布局复用,在更改某个组件时就可以做到改一个布局文件就更改了应用中所有引用该布局文件的组件,做到一改全改。
<include/>
<include/>标签在布局优化中是使用最多的一个标签了,它就是为了解决重复定义布局的问题。<include/>标签就相当于C、C++中的include头文件一样,把一些常用的底层的API封装起来,需要的时候引入即可。在一些开源的J2EE中许多XML配置文件也都会使用<include/>标签,将多个配置文件组合成为一个更为复杂的配置文件,如最常见的S2SH。
在以前Android开发中,由于ActionBar设计上的不统一以及兼容性问题,所以很多应用都自定义了一套自己的标题栏titlebar。标题栏我们知道在应用的每个界面几乎都会用到,在这里可以作为一个很好的示例来解释<include/>标签的使用。
下面是一个自定义的titlebar文件:
'<FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/titlebar_bg"> <ImageViewandroid:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/gafricalogo"/></FrameLayout>'

在应用中使用titlebar布局文件,我们通过<include/>标签,布局文件如下:
<LinearLayoutxmlns: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"> <includelayout="@layout/titlebar"/> <TextViewandroid:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/hello" android:padding="10dp"/> ... </LinearLayout>

在<include/>标签中可以覆盖导入的布局文件root布局的布局属性(如layout_*属性)。
布局示例如下:
<includeandroid: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文件:
<LinearLayoutxmlns: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>

一个线性布局中嵌套一个文本视图,主布局如下:
<LinearLayoutxmlns: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布局文件如下:
<mergexmlns: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布局文件:
<LinearLayoutxmlns: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类似):
<LinearLayoutxmlns: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代码如下:
private ViewStubviewStub; protected void onCreate(BundlesavedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_main2); viewStub = (ViewStub) findViewById(R.id.stub_image); //viewStub.inflate();//方式一 viewStub.setVisibility(View.VISIBLE);//方式二 ImageViewimageView = (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源码
/
** Exercise <include /> tag in XML files.*/public class Include extends Activity { @Override protected void onCreate(Bundleicicle) { super.onCreate(icicle); setContentView(R.layout.include_tag); }}

Merge源码
/*** Exercise <merge /> tag in XML files.*/public class Merge extends Activity { private LinearLayoutmLayout; @Override protected void onCreate(Bundleicicle) { super.onCreate(icicle); mLayout = new LinearLayout(this); mLayout.setOrientation(LinearLayout.VERTICAL); LayoutInflater.from(this).inflate(R.layout.merge_tag, mLayout); setContentView(mLayout); } public ViewGroupgetLayout() { return mLayout; }}

来自: http://www.sunnyang.com/418.html

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 151,688评论 1 330
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 64,559评论 1 273
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 101,749评论 0 226
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 42,581评论 0 191
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 50,741评论 3 271
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 39,684评论 1 192
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,122评论 2 292
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 29,847评论 0 182
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 33,441评论 0 228
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 29,939评论 2 232
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,333评论 1 242
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 27,783评论 2 236
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,275评论 3 220
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,830评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,444评论 0 180
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 34,553评论 2 249
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 34,618评论 2 249

推荐阅读更多精彩内容