Android界面布局的优化:<merge/>、<include/>和<ViewStub/>标签的使用

在上一节中,当用layoutopt进行优化分析的时候,它提示可以将<FrameLayout/>换成<merge/>。其实<merge/>标签在UI的结构优化中起着非常重要的作用,通过它可以删减多余的层级,达到优化UI的目的。

再来看一下EX_03_07这个框架布局的树形结构,如图3-27所示。

android 标签布局 源码 android标签页布局_Android

 


图3-27  框架布局例子树形结构图

根节点和LinearLayout上面那个子树为Android的窗口布局,后面的TextView即为标签。大框内的这部分树形图即为我们的布局。可以发现小框框出的FrameLayout是另一个FrameLayout的唯一子元素,并且该父节点并没有额外的属性,也就是说,它是完全冗余的,这时候就要用<merge/>标签来解决这个问题。用<merge/>替换原来的<FrameLayout/>标签,修改后的布局代码如下:

1. <?xmlversionxmlversion="1.0"encoding="utf-8"?> 
2. <mergexmlns:androidmergexmlns:android="http://schemas.android.com/apk/res/android" 
3. android:orientation="vertical" 
4. android:layout_width="fill_parent" 
5. android:layout_height="fill_parent" 
6. > 
7. <TextView 
8. android:layout_width="300dip" 
9. android:layout_height="300dip" 
10. android:background="#00008B" 
11. android:layout_gravity="center" 
12. /> 
13. <TextView 
14. android:layout_width="250dip" 
15. android:layout_height="250dip" 
16. android:background="#0000CD" 
17. android:layout_gravity="center" 
18. /> 
19. <TextView 
20. android:layout_width="200dip" 
21. android:layout_height="200dip" 
22. android:background="#0000FF" 
23. android:layout_gravity="center" 
24. /> 
25. <TextView 
26. android:layout_width="150dip" 
27. android:layout_height="150dip" 
28. android:background="#00BFFF" 
29. android:layout_gravity="center" 
30. /> 
31. <TextView 
32. android:layout_width="100dip" 
33. android:layout_height="100dip" 
34. android:background="#00CED1" 
35. android:layout_gravity="center" 
36. /> 
37. </merge>


再观察一下它的树形图,如图3-28所示,显然层次更简单了。

为什么会这样呢,因为Activity的根节点都是FrameLayout,所以用merge标签可以直接添加到这个FrameLayout,而不要再增加一个FrameLayout节点。但是,如果布局是以LinearLayout等为根节点,就不能这么做了。

<merge/>其实还有很多作用,它和<include/>标签就能完美的结合。<include/>标签用来实现代码的重用以及布局的模块化。如果UI中需要多次用到同一个布局,<include/>标签的引用会大大提高开发效率。

android 标签布局 源码 android标签页布局_xml_02

 


(点击查看大图)图3-28  修改后的框架布局例子的树形图

实例:

新建一个共享布局:share.xml


    1. <?xmlversionxmlversion="1.0"encoding="utf-8"?> 
    2. <LinearLayoutxmlns:androidLinearLayoutxmlns:android="
    http://schemas.android.com/apk/res/android" 3. android:orientation="vertical" 
    4. android:layout_width="fill_parent" 
    5. android:layout_height="fill_parent" 
    6. > 
    7. <TextView 
    8. android:layout_width="fill_parent" 
    9. android:layout_height="wrap_content" 
    10. android:text="这是一个共享布局" 
    11. /> 
    12. </LinearLayout> 
    13.


    Android界面布局的优化(2)

    然后在需要引用这个布局的布局中使用<include/>标签,并且可以重写它的一些属性(下面的代码就重写了它的id):

    1. <?xmlversionxmlversion="1.0"encoding="utf-8"?> 
    2. <LinearLayoutxmlns:androidLinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" 
    3. android:orientation="vertical" 
    4. android:layout_width="fill_parent" 
    5. android:layout_height="fill_parent"> 
    6. <include android:id="@+id/new" layout="@layout/share"></include> 
    7. </LinearLayout>



    如果在布局中只需要使用共享布局的内容,这时候就可以用merge标签,这样使得布局更加高效灵活。

    1. <?xmlversionxmlversion="1.0"encoding="utf-8"?> 
    2. <mergexmlns:androidmergexmlns:android="http://schemas.android.com/apk/res/android" 
    3. <include android:id="@+id/newone" layout="@layout/share"></include> 
    4. <include android:id="@+id/newtwo" layout="@layout/share"></include> 
    5. </merge>


    有了<include/>标签,很容易就能做到共享和重用布局,可是很多情况下,一个布局中有很多View并不常用,这造成了资源的浪费,Android为此提供了ViewStub标签来解决这个问题。在默认情况下ViewStub下的标签都有visibility=GONE属性(不可见),更重要的是,在这个标签下的内容不会占用任何的空间。其实ViewStub和include类似,区别就在于ViewStub只会在你需要的时候进入界面,ViewStub通过inflate()方法来通知系统加载其内部的View。这样就可以让我们既享受到<include/>的便利,又不会产生过多没用的View。

    实例:

    工程目录:EX_03_08

    其中share.xml前面已经介绍过了,main.xml的布局文件:

    1. <?xmlversionxmlversion="1.0"encoding="utf-8"?> 
    2. <LinearLayoutxmlns:androidLinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" 
    3. android:orientation="vertical" 
    4. android:layout_width="fill_parent" 
    5. android:layout_height="fill_parent" 
    6. > 
    7. <Button 
    8.     android:id="@+id/show" 
    9.     android:layout_width="wrap_content" 
    10.     android:layout_height="wrap_content" 
    11.     android:text="点击导入" 
    12.     /> 
    13. <ViewStub 
    14. android:id="@+id/viewStub" 
    15. android:layout="@layout/share" 
    16. android:layout_width="wrap_content" 
    17. android:layout_height="wrap_content" 
    18. /> 
    19. </LinearLayout> 
    20.  
    21. MyViewStub.java代码:  
    22. package com.pms.viewstub;  
    23.  
    24. import android.app.Activity;  
    25. import android.os.Bundle;  
    26. import android.view.View;  
    27. import android.view.ViewStub;  
    28. import android.view.View.OnClickListener;  
    29. import android.widget.Button;  
    30.  
    31. publicclass MyViewStub extends Activity {  
    32.       
    33.     private ViewStub mViewStub;  
    34.     private Button   showButton;  
    35.       
    36. /**Calledwhentheactivityisfirstcreated.*/  
    37. @Override  
    38. publicvoid onCreate(Bundle savedInstanceState) {  
    39. super.onCreate(savedInstanceState);  
    40.         setContentView(R.layout.main);  
    41.  
    42. mViewStub = (ViewStub)findViewById(R.id.viewStub);//实例化ViewStub控件,这里可以看出我们必//须为ViewStub设定id  
    43. showButton = (Button)findViewById(R.id.show);  
    44.  
    45. /*为按钮添加点击监听事件,后面的章节会介绍*/                  
    46. showButton.setOnClickListener(new OnClickListener(){  
    47.       
    48.     @Override  
    49.     publicvoid onClick(View v) {  
    50.         if (mViewStub != null) {    
    51. mViewStub.inflate();  //点击即导入ViewStub标签的内容  
    52.                   }    
    53.     }  
    54.         });  
    55.     }  
    56. }


    运行效果如图3-29所示,当点击按钮后,导入的布局就会显示,如图3-30所示。这里要注意的是,其实很多时候不需要保留ViewStub的引用(这个例子中,在字段里保留了ViewStub的引用),因为当ViewStub 执行inflate后,这个ViewStub就从View层次中移除了。在读者深入学习后,会经常用到infate()方法来导入布局加载到原来的View上,那时候会发现用ViewStub是个更好的办法。但是要注意的是,ViewStub还不支持<merge/>标签。

    android 标签布局 源码 android标签页布局_Android_03

     


    图3-29  ViewStub例子程序

    android 标签布局 源码 android标签页布局_android 标签布局 源码_04

     


    图3-30  点击按钮后导入ViewStub内容