include和merge 布局使减少布局重用

来源:互联网 发布:淘宝商城天猫首页 编辑:程序博客网 时间:2024/04/30 04:28


尽管Android 支持各种小部件,来提供小且可以重用的交互元素,你可能还需要更大的,要求一个专门布局的重用组件。为了高效的重用整个布局,你能使用<include/>和<merge/>标签在当前的布局中嵌入别的布局。 

 

重用布局功能特别强大,因为它允许你创建可重用的复杂布局。例如,一个yes/no按钮面板,或者自定义带有描述字符串的滚动条。也就是说,在你的应用中任何跨越多个布局的相同元素都能被提取,单独管理,然后被包含在每个布局中。所以当你通过编写一个自定义View创建独特的UI组件的时候,通过重用一个布局实现它或许更容易 。

 

创建一个重用布局 

——————————————————————————————————————————————————————————————

如果你已经知道了你想重用的布局,创建一个新的XML文件定义它。例如,这里有一个自G-Kenya codelag的布局,它定义了标题栏,被包含在每个Activity中(titlebar.xml)。 

[html] view plain copy
  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"   
  2.     android:layout_width=”match_parent”   
  3.     android:layout_height="wrap_content"   
  4.     android:background="@color/titlebar_bg">   
  5.    
  6.     <ImageView android:layout_width="wrap_content"   
  7.                android:layout_height="wrap_content"    
  8.                android:src="@drawable/gafricalogo" />   
  9. </FrameLayout>   

在每个添加这个布局的布局中,根View决定了你想如何显示它

使用<include>标识 

——————————————————————————————————————————————————————————————

在你想添加这个重用组件的布局中,添加<include/>标签。例如,下面的布局来自G-Kenya codelab,它包含了上面提到的标题栏 

下面是布局文件: 


[html] view plain copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
  2.     android:orientation="vertical"    
  3.     android:layout_width=”match_parent”   
  4.     android:layout_height=”match_parent”   
  5.     android:background="@color/app_bg"   
  6.     android:gravity="center_horizontal">   
  7.     <include layout="@layout/titlebar"/>   
  8.     <TextView android:layout_width=”match_parent”   
  9.               android:layout_height="wrap_content"   
  10.               android:text="@string/hello"   
  11.               android:padding="10dp" />   
  12.     …   
  13. </LinearLayout>   

通过在<include/>标签中指定它们,你能覆盖被包含布局根视图的所有布局参数(任何android:layout_*属性)。例如: 

[html] view plain copy
  1. <include android:id=”@+id/news_title”   
  2.          android:layout_width=”match_parent”   
  3.          android:layout_height=”match_parent”   
  4.          layout=”@layout/title”/>   

不管怎样,如果你在使用<include>标签时覆盖布局属性,为了对别的布局属性生效,你必须都覆盖android:layout_height和android:layout_width属性。

使用<merge>标识 

—————————————————————————————————————————— 

当一个布局包含另一个时,<merge/>标签帮助消除在你的视图结构中冗余的视图组。例如,如果你的主布局是一个竖直的LinearLayout,在其中两个连续的视图可以在多个布局中重用,那么你使用的这两个重用的视图需要自己的根视图。无论如何,使用其它LinearLayout作为重用布局的根节点,会导致一个竖直LinearLayout嵌套一个竖直LinearLayout中。这个嵌入的LinearLayout不仅没到启到真正的作用,反而减缓了你的UI性能。  

 

为了避免包含这样一个多余的视图组,你能使用<merge>元素替代这个重用布局的的根视图。例如: 

[html] view plain copy
  1. <merge xmlns:android="http://schemas.android.com/apk/res/android">   
  2.     <Button   
  3.         android:layout_width="fill_parent"    
  4.         android:layout_height="wrap_content"   
  5.         android:text="@string/add"/>   
  6.     <Button   
  7.         android:layout_width="fill_parent"    
  8.         android:layout_height="wrap_content"   
  9.         android:text="@string/delete"/>   
  10. </merge>   

现在,当你在其它的布局中包含这个布局的时候(使用<include/>标签),系统不顾这个<merge>元素并直接在这个布局中放置这两个按钮,取代<include/>标签。

转载与:http://blog.csdn.net/p106786860/article/details/12677029

0 0