ViewStub、Include以及Merge

ViewStub

用法

ViewStub是一个轻量级的View,没有大小,不会绘制且不占用布局的控件。ViewStub易于inflate布局或者离开布局。ViewStub最佳的描述是“懒惰的include”(这个称呼决定了这次要把include好好了解下),ViewStub中引用的布局只在你想添加到UI上时才会显示。各种不常用的布局进度条,显示错误消息,信息详情都可以使用标签,以较少内存的使用量,加快渲染速度。
例子:在ListView中加入EmptyView,即当ListView的adapter中没有数据时,加载一个显示没有数据的布局。

<ViewStub
android:id="@+id/empty_view_stub"
android:inflatedId="@+id/empty_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout="@layout/layout_empty_view" />

当想加载布局时,可以使用下面的方法:

((ViewStub)findViewById(R.id.empty_view_stub)).setVisibility(View.VISIBLE);
或者
View emptyView=((ViewStub)findViewById(R.id.empty_view_stub)).inflate();

当调用inflate()函数时,ViewStub被引用的资源替代,并且返回引用的view,所以想与布局进行交互时,不需要再进行findViewById()。
一旦ViewStub被Visible或者Inflated,ViewStub元素不再是View视图的一部分。它被引用的资源替换,根布局的ID即为ViewStub属性中android:inflatedId指定的ID。
只有当布局Visible或者Inflated的时候,ViewStub中指定的android:id的属性值才是有效的。
注:ViewStub目前不支持标签的使用。

实例

根据一个实例,来解释下面两句话的含义。
1、ViewStub是一个轻量级的View,没有大小,不会绘制且不占用布局的控件。ViewStub中引用的布局只在你想添加到UI上时才会显示。
2、 一旦ViewStub被Visible或者Inflated,ViewStub元素不再是View视图的一部分。它被引用的资源替换,根布局的ID即为ViewStub属性中android:inflatedId指定的ID。
首先,我在一个layout_main.xml文件中定义如下的布局。然后,点击按钮,设置ViewStub visible或者inflate,来显示ViewStub中的布局。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ClickMe"/>
<ViewStub
android:id="@+id/viewStub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inflatedId="@+id/root_viewstub"
android:layout="@layout/layout_view_sub" />
</LinearLayout>

ViewStub中引用的布局文件如下所示。

<?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="match_parent"
android:orientation="vertical">
<TextView>
android:id="@+id/hello"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="hello view stub"/>
</LinearLayout>

在MainActivity中我们写这样的功能:

private Button button;
private ViewStub viewStub;
private View rootViewStub;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId()==R.id.button){
rootViewStub=((ViewStub)findViewById(R.id.viewStub)).inflate();
textView=(TextView) rootViewStub.findViewById(R.id.hello);
textView.setText("Hello CQ");
}
}

在点击按钮之前,运行Activity获得的布局层级如下图所示,


可以看到没有ViewStub,解释了第一句话,ViewStub是一个轻量级的View,没有大小,不会绘制且不占用布局的控件。
点击按钮之后,重新获得布局的层级如下图所示,


可以看到还是没有ViewStub这个控件,但是有ViewStub中的布局LinearLayout和TextView,代替ViewStub的存在,并且LinearLayout作为跟节点。LinearLayout的资源ID如下图所示。


该资源ID正好是ViewStub中inflateId属性的值。
这就解释了:2、 一旦ViewStub被Visible或者Inflated,ViewStub元素不再是View视图的一部分。它被引用的资源替换,根布局的ID即为ViewStub属性中android:inflatedId指定的ID。
假如,你在LinearLayout中自己加了一个@+id/name,发现只要你在ViewStub中指定了android:inflatedId,LinearLayout的资源ID就是inflatedId的属性值。

Include

用法

include标签能够重用视图文件,解决了重复定义相同布局文件的问题。

<include
android:id="@+id/include_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="@layout/layout_include" />

注意 :
layout="@layout/layout_include" 不要写成android:layout="@layout/layout_include"这样会引发以下的错误 android.view.InflateException,看提示也知道自己要改哪里了。


实例

我定义了一个layout_include文件如下所示。

<?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="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ClickMe"/>
</LinearLayout>

在要复用的布局中加入代码。运行之后,得到的视图层级如下所示。


发现layout_include的布局直接被加进来了。
所以这个还是很容易使用的,那我们结合ViewStub来总结下。

总结

通过上面的实例分析,发现ViewStub与include还真是挺像的,所以ViewStub被称为“懒惰的include”还真是恰当,因为在渲染的时候,ViewStub不占用,不显示在根布局中,只有在inflate之后,才和include体现了相似的功能,将layout中的所指定的布局显示在根布局中。

Merge

用法

标签在UI的结构优化中起着非常重要的作用,它可以删减多余的层级,优化UI。多用于替换FrameLayout或者当一个布局包含另一个时,标签消除视图层次结构中多余的视图组。标签在你嵌套 Layout 时取消了 UI 层级中冗余的 ViewGroup 。比如,如果你有一个 Layout 是一个竖直方向的 LinearLayout,其中包含两个连续的 View 可以在别的 Layout 中重用,那么你会做一个 LinearLayout 来包含这两个 View ,以便重用。不过,当使用一个 LinearLayout 作为另一个 LinearLayout 的根节点时,这种嵌套 LinearLayout 的方式除了减慢你的 UI 性能外没有任何意义。

实例

定义一个布局layout_merge.xml

<?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="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/mergeTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="merge"/>
</LinearLayout>

然后我们复用上面include中的代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="@+id/include_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="@layout/layout_merge" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ClickMe" />
</LinearLayout

运行下代码,查看视图层级如下所示。


我们发现layout_merge中的布局的最外层LinearLayout,也是垂直分布的,与我们要include进去的根布局的最外层LinearLayout的分布是一样的。所以我们可以将布局改写一下。

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/mergeTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="merge"/>
</merge>

使用merge标签后,视图层级如下所示。


很明显的我们减少了一层LinearLayout。

参考链接

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

推荐阅读更多精彩内容