布局优化利器<include/>和ViewStub

转载自:http://blog.csdn.net/manoel/article/details/39036507

本文翻译自《50 Android hacks》


当创建复杂的布局的时候,有时候会发现添加了很多的ViewGroup和View。随之而来的问题是View树的层次越来越深,应用也变的越来越慢,因为UI渲染是非常耗时的。

这时候就应该进行布局优化了。这里介绍两种方式,分别为<include>标签和ViewStub类。


<include/>

使用<include/>是为了避免代码的重复。设想一种情况,我们需要为app中的每个视图都添加一个footer,这个footer是一个显示app名字的TextView。通常多个Activity对应多个XML布局文件,难道要把这个TextView复制到每个XML中吗?如果TextView需要做修改,那么每个XML布局文件都要进行修改,那简直是噩梦。

面向对象编程的其中一个思想就是代码的复用,那么怎么进行布局的复用呢?这时,<include/>就起作用了。

如果学过C语言,那么对#include应该不陌生,它是一个预编译指令,在程序编译成二进制文件之前,会把#include的内容拷贝到#include的位置。

Android中的<include/>也可以这么理解,就是把某些通用的xml代码拷贝到<include/>所在的地方。以一个Activity为例。

[html]  view plain  copy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent"  
  2.     android:layout_height="fill_parent" >  
  3.     <TextView  
  4.         android:layout_width="fill_parent"  
  5.         android:layout_height="wrap_content"  
  6.         android:layout_centerInParent="true"  
  7.         android:gravity="center_horizontal"  
  8.         android:text="@string/hello" />  
  9.       
  10.     <include layout="@layout/footer_with_layout_properties"/>  
  11.   
  12.   
  13. </RelativeLayout>  
footer_with_layout_properties.xml中就是一个简单的TextView,代码如下:

[html]  view plain  copy
  1. <TextView xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="fill_parent"  
  3.     android:layout_height="wrap_content"  
  4.     android:layout_alignParentBottom="true"  
  5.     android:layout_marginBottom="30dp"  
  6.     android:gravity="center_horizontal"  
  7.     android:text="@string/footer_text" />  
上述的代码中,我们使用了<include/>标签,达到了代码复用的目的。

但是,仍然存在一些疑惑。

footer_with_layout_properties.xml中使用了android:layout_alignParentBottom属性,这个属性之所以可行,是因为外层布局是RelativeLayout。

那么,如果外层布局换做LinearLayout又会怎样呢?答案显而易见,这肯定是行不通的。那么怎么办呢?我们可以把具体的属性写在<include/>标签里面,看下面的代码。

[html]  view plain  copy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent"  
  2.     android:layout_height="fill_parent">  
  3.     <TextView  
  4.         android:layout_width="fill_parent"  
  5.         android:layout_height="wrap_content"  
  6.         android:layout_centerInParent="true"  
  7.         android:gravity="center_horizontal"  
  8.         android:text="@string/hello"/>  
  9.     <include  
  10.         layout="@layout/footer"  
  11.         android:layout_width="fill_parent"  
  12.         android:layout_height="wrap_content"  
  13.         android:layout_alignParentBottom="true"  
  14.         android:layout_marginBottom="30dp"/>  
  15. </RelativeLayout>  
我们直接在<include/>标签里面使用了android:layout_*属性。

注意:如果想要在<include/>标签中覆盖被包含布局所指定的任何android:layout_*属性,必须在<include/>标签中同时指定layout_width和layout_height属性,这可能是一个Android系统的一个bug吧。


ViewStub
在开发过程中,难免会遇到各种交互问题,例如显示或隐藏某个视图。如果想要一个视图只在需要的时候显示,可以尝试使用ViewStub这个类。

先看一下ViewStub的官方介绍:

“ViewStub是一个不可视并且大小为0的视图,可以延迟到运行时填充布局资源。当ViewStub设置为Visible或调用inflate()之后,就会填充布局资源,ViewStub便会被填充的视图替代”。

现在已经清楚ViewStub能干什么了,那么看一个例子。一个布局中,存在一个MapView,只有需要它的时候,才让它显示出来。

[html]  view plain  copy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="fill_parent"  
  3.     android:layout_height="fill_parent" >  
  4.   
  5.     <Button  
  6.         android:layout_width="fill_parent"  
  7.         android:layout_height="wrap_content"  
  8.         android:layout_gravity="center_vertical"  
  9.         android:onClick="onShowMap"  
  10.         android:text="@string/show_map" />  
  11.   
  12.     <ViewStub  
  13.         android:id="@+id/map_stub"  
  14.         android:layout_width="fill_parent"  
  15.         android:layout_height="fill_parent"  
  16.         android:inflatedId="@+id/map_view"  
  17.         android:layout="@layout/map" />  
  18.   
  19. </RelativeLayout>  

map.xml文件中包含一个MapView,只有在必要的时候,才会让它显示出来。

[html]  view plain  copy
  1. <com.google.android.maps.MapView xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:id="@+id/map_view"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:apiKey="my_api_key"  
  6.     android:clickable="true" />  

另外,inflatedId是ViewStub被设置成Visible或调用inflate()方法后返回的id,这个id就是被填充的View的id。在这个例子中,就是MapView的id。

接下来看看ViewStub是怎么使用的。

[java]  view plain  copy
  1. public class MainActivity extends MapActivity {  
  2.   
  3.   private View mViewStub;  
  4.   
  5.   @Override  
  6.   public void onCreate(Bundle savedInstanceState) {  
  7.     super.onCreate(savedInstanceState);  
  8.     setContentView(R.layout.main);  
  9.     mViewStub = findViewById(R.id.map_stub);  
  10.   }  
  11.   
  12.   public void onShowMap(View v) {  
  13.     mViewStub.setVisibility(View.VISIBLE);  
  14.   }  
  15.   
  16.   @Override  
  17.   protected boolean isRouteDisplayed() {  
  18.     return false;  
  19.   }  
  20. }  


题外话

有的同学肯定会问,使用ViewStub和单纯地把View设置为View.GONE或View.VISIBLE有什么区别呢?不都是显示和隐藏吗,使用ViewStub反而更麻烦了。

确实是有区别的,会涉及到View树的渲染,内存消耗等。

至于有什么具体的差别,就请大家自己去Google吧。俗话说,自己动手,丰衣足食嘛!


参考资料

http://code.google.com/p/android/issues/detail?id=2863

http://android-developers.blogspot.com.ar/2009/03/android-layout-tricks-3-optimize-with.html

http://developer.android.com/reference/android/view/ViewStub.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值