利用<include />标签来避免重复渲染

当你在Application中创建复杂的布局时,页面的渲染过程也变得更加缓慢。
此时,我们需要利用 <include />标签(避免重复渲染)和 ViewStub类(延迟加载)来优化我们的页面。
一、利用<include />标签来避免重复渲染

当我们需要为App中的每个View都添加一个header或者footer时,你会怎么做?
重复地复制粘贴可以解决这个问题,但未免太繁杂。可以试着使用<include />标签:

第一种方式,在<include />标签内指定width及height:
main.xml
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  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.     <include  
  13.         android:layout_width ="fill_parent"  
  14.         android:layout_height ="wrap_content"  
  15.         android:layout_alignParentBottom ="true"  
  16.         android:layout_marginBottom ="30dp"  
  17.         layout ="@layout/footer" />  
  18.   
  19. </RelativeLayout>  

footer.xml
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <TextView xmlns:android = "http://schemas.android.com/apk/res/android"  
  2.     android:layout_width"0dp"  
  3.     android:layout_height"0dp"  
  4.     android:gravity"center"  
  5.     android:text"@string/footer_text" />  

有个小细节需要注意,在footer.xml中,我们将width及height都设为0dp.目的是为了配合<include/>标签中对width及height的定义

第二种方式,直接引用:
main.xml
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  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.     <include layout ="@layout/footer" />  
  13.   
  14. </RelativeLayout>  

footer.xml
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  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"  
  7.     android:text"@string/footer_text" />  


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值