Android界面布局的优化:<merge/>、<include/>和<ViewStub/>标签的使用
在上一节中,当用layoutopt进行优化分析的时候,它提示可以将<FrameLayout/>换成<merge/>。其实<merge/>标签在UI的结构优化中起着非常重要的作用,通过它可以删减多余的层级,达到优化UI的目的。
再来看一下EX_03_07这个框架布局的树形结构,如图3-27所示。
图3-27 框架布局例子树形结构图 |
- <?xmlversionxmlversion="1.0"encoding="utf-8"?>
- <mergexmlns:androidmergexmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="300dip"
- android:layout_height="300dip"
- android:background="#00008B"
- android:layout_gravity="center"
- />
- <TextView
- android:layout_width="250dip"
- android:layout_height="250dip"
- android:background="#0000CD"
- android:layout_gravity="center"
- />
- <TextView
- android:layout_width="200dip"
- android:layout_height="200dip"
- android:background="#0000FF"
- android:layout_gravity="center"
- />
- <TextView
- android:layout_width="150dip"
- android:layout_height="150dip"
- android:background="#00BFFF"
- android:layout_gravity="center"
- />
- <TextView
- android:layout_width="100dip"
- android:layout_height="100dip"
- android:background="#00CED1"
- android:layout_gravity="center"
- />
- </merge>
再观察一下它的树形图,如图3-28所示,显然层次更简单了。
为什么会这样呢,因为Activity的根节点都是FrameLayout,所以用merge标签可以直接添加到这个FrameLayout,而不要再增加一个FrameLayout节点。但是,如果布局是以LinearLayout等为根节点,就不能这么做了。
<merge/>其实还有很多作用,它和<include/>标签就能完美的结合。<include/>标签用来实现代码的重用以及布局的模块化。如果UI中需要多次用到同一个布局,<include/>标签的引用会大大提高开发效率。
(点击查看大图)图3-28 修改后的框架布局例子的树形图 |
实例:
新建一个共享布局:share.xml
- <?xmlversionxmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:androidLinearLayoutxmlns:android="
http://schemas.android.com/apk/res/android"- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="这是一个共享布局"
- />
- </LinearLayout>
Android界面布局的优化(2)
然后在需要引用这个布局的布局中使用<include/>标签,并且可以重写它的一些属性(下面的代码就重写了它的id):
- <?xmlversionxmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:androidLinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <include android:id="@+id/new" layout="@layout/share"></include>
- </LinearLayout>
如果在布局中只需要使用共享布局的内容,这时候就可以用merge标签,这样使得布局更加高效灵活。
- <?xmlversionxmlversion="1.0"encoding="utf-8"?>
- <mergexmlns:androidmergexmlns:android="http://schemas.android.com/apk/res/android"
- <include android:id="@+id/newone" layout="@layout/share"></include>
- <include android:id="@+id/newtwo" layout="@layout/share"></include>
- </merge>
有了<include/>标签,很容易就能做到共享和重用布局,可是很多情况下,一个布局中有很多View并不常用,这造成了资源的浪费,Android为此提供了ViewStub标签来解决这个问题。在默认情况下ViewStub下的标签都有visibility=GONE属性(不可见),更重要的是,在这个标签下的内容不会占用任何的空间。其实ViewStub和include类似,区别就在于ViewStub只会在你需要的时候进入界面,ViewStub通过inflate()方法来通知系统加载其内部的View。这样就可以让我们既享受到<include/>的便利,又不会产生过多没用的View。
实例:
工程目录:EX_03_08
其中share.xml前面已经介绍过了,main.xml的布局文件:
- <?xmlversionxmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:androidLinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <Button
- android:id="@+id/show"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="点击导入"
- />
- <ViewStub
- android:id="@+id/viewStub"
- android:layout="@layout/share"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
- MyViewStub.java代码:
- package com.pms.viewstub;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.ViewStub;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- publicclass MyViewStub extends Activity {
- private ViewStub mViewStub;
- private Button showButton;
- /**Calledwhentheactivityisfirstcreated.*/
- @Override
- publicvoid onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mViewStub = (ViewStub)findViewById(R.id.viewStub);//实例化ViewStub控件,这里可以看出我们必//须为ViewStub设定id
- showButton = (Button)findViewById(R.id.show);
- /*为按钮添加点击监听事件,后面的章节会介绍*/
- showButton.setOnClickListener(new OnClickListener(){
- @Override
- publicvoid onClick(View v) {
- if (mViewStub != null) {
- mViewStub.inflate(); //点击即导入ViewStub标签的内容
- }
- }
- });
- }
- }
运行效果如图3-29所示,当点击按钮后,导入的布局就会显示,如图3-30所示。这里要注意的是,其实很多时候不需要保留ViewStub的引用(这个例子中,在字段里保留了ViewStub的引用),因为当ViewStub 执行inflate后,这个ViewStub就从View层次中移除了。在读者深入学习后,会经常用到infate()方法来导入布局加载到原来的View上,那时候会发现用ViewStub是个更好的办法。但是要注意的是,ViewStub还不支持<merge/>标签。
图3-29 ViewStub例子程序 |
图3-30 点击按钮后导入ViewStub内容 |