[Android 性能优化系列]布局篇之通过<include>复用布局

来源:互联网 发布:一键安装java环境 编辑:程序博客网 时间:2024/06/06 17:58

原文地址:http://developer.android.com/training/improving-layouts/reusing-layouts.html

很多时候,我们都会用到类似的布局,既然如此,我们不妨将相同布局整体抽出来,单独作为一个布局文件使用,这样我们就避免了在多个文件中反复书写同样地代码,并且当我们需要修改的时候,也只需要修改一个地方就好了。

虽然安卓为我们提供了一系列的控件来方便我们进行交互,你或许还是需要重复使用到一些特定布局的大型组件。为了更有效的复用布局,你应该使用<include/>和<merge/>来让一个布局出现在另一个布局中,而不是在每一个布局文件中都重写他。

在这种情况下,复用布局是格外有用的,他允许你创建一个复杂的可重用布局。比如说,一个 yes/no 按钮,一个拥有文字的自定义进度条。这也意味着,你应用中的一些元素是通用的。所以你可以单独为他们创建一个自定义 View,这样一来你可以更方便的重用布局


建立一个可重用的布局

如果你已经知道哪些布局你希望能够反复使用,那么单独为他们创建一个新的布局文件吧。比如说,这里有一个来自 G-Kenya 代码实验室的布局,它定义了一个标题栏,而这个标题栏会被每一个 activity 所引用

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  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 应该同你希望他在每个 Activity 中的显示效果一致。


使用<include>标签

当你希望添加一个可重用的组件到另一个布局中时,你可以使用<include/>标签。比如说,这里有一段来自 G-Kenya 代码实验室的代码,他想要包含了上面提到的标题栏

这里是他的布局文件

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  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.   
  8.     <include layout="@layout/titlebar"/>  
  9.   
  10.     <TextView android:layout_width=”match_parent”  
  11.               android:layout_height="wrap_content"  
  12.               android:text="@string/hello"  
  13.               android:padding="10dp" />  
  14.   
  15.     ...  
  16.   
  17. </LinearLayout>  

同样的,你可以在<include/>标签内重写布局文件的参数,比如说类似于 android:layout_*的属性,来定义包含的布局的属性,例如

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <include android:id=”@+id/news_title”  
  2.          android:layout_width=”match_parent”  
  3.          android:layout_height=”match_parent”  
  4.          layout=”@layout/title”/>  

不管如何,如果你希望使用<include>标签重写布局属性,你必须要重写 layout_height 和 layout_width 来让其他属性生效


使用<merge>标签

<merge/>标签在能够有效的帮助我们降低你的布局层级。比如说,你的布局是一个垂直的线性布局,并且你希望复用的布局也是一个类似的垂直的线性布局。那么,使用另外一个线性布局作为重用布局的根元素会导致一个垂直的线性布局中包含另一个垂直的线性布局。这种嵌套的线性布局没有任何意义,并且会降低你的 ui 性能

为了避免上面的情况发生,你可以在你重用的布局中使用<merge>标签作为根元素,例如下面这样

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <merge xmlns:android="http://schemas.android.com/apk/res/android">  
  2.   
  3.     <Button  
  4.         android:layout_width="fill_parent"   
  5.         android:layout_height="wrap_content"  
  6.         android:text="@string/add"/>  
  7.   
  8.     <Button  
  9.         android:layout_width="fill_parent"   
  10.         android:layout_height="wrap_content"  
  11.         android:text="@string/delete"/>  
  12.   
  13. </merge>  
这样一来,当你使用<include/>标签进行复用的时候,系统会忽略掉<merge>标签,然后就将两个 Button 按钮放到<include/>的位置里去
0 0
原创粉丝点击