本文原创, 转载请注明出处:http://blog.csdn.net/qinjuning

 

译二:

 

使用<include />标签复用布局文件

 
 

 翻译地址:http://developer.android.com/training/improving-layouts/reusing-layouts.html#Merge

尽管Android通过内置了各种各样的控件提供了微小、可复用的交互性元素,也许你需要复用较大的

组件 ---- 某些特定布局文件 。为了更有效率复用的布局文件,你可以使用<include />以及<merge />

标签将其他的布局文件加入到当前的布局文件中。

 

复用布局文件是一种特别强大的方法,它允许你创建可复用性的布局文件。例如,一个包含“Yse”or“No”的

Button面版,或者是带有文字说明的 Progressbar。复用布局文件同样意味着你应用程序里的任何元素都能从

繁杂的布局文件提取出来进行单独管理,接着你需要做的只是加入这些独立的布局文件(因为他们都是可复用地)。

因此,当你通过自定义View创建独立的UI组件时,你可以复用布局文件让事情变得更简单。

1、创建一个可复用性的布局文件

如果你已经知道复用布局的”面貌”,那么创建、定义布局文件( 命名以”.xml”为后缀)。例如,这里是一个来自

G- Kenya codelab 的布局文件,定义了在每个Activity中都要使用的一个自定义标题 (titlebar.xml):由于这些

可复用性布局被添加至其他布局文件中,因此,它的每个根视图(root View)最好是精确(exactly)的。

  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. <ImageView android:layout_width="wrap_content"
  6. android:layout_height="wrap_content"
  7. android:src="@drawable/gafricalogo" />
  8. </FrameLayout>

2、使用<include />标签

 

在需要添加这些布局的地方,使用<include />标签 。 例如,下面是一个来自G-Kenya codelab的布局文件,

它复用了上面列出的“title bar”文件, 该布局文件如下:

  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 />节点中为被添加的布局文件的root View定义特别标识,重写所有layout参数即可(任何

以“android:layout_”为前缀的属性)。例如:

  1. <include android:id=”@+id/news_title”
  2. android:layout_width=”match_parent”
  3. android:layout_height=”match_parent”
  4. layout=”@layout/title”/>

3、使用<merge />标签

 

当在布局文件中复用另外的布局时, <merge />标签能够在布局层次消除多余的视图元素。例如,如果你的

主布局文件是一个垂直地包含两个View的LinearLayout,该布局能够复用在其他布局中,而对任意包含两个View的

布局文件都需要一个root View(否则, 编译器会提示错误)。然而,在该可复用性布局中添加一个LinearLayout

作为root View,将会导致一个垂直的LinearLayout包含另外的垂直LinearLayout。内嵌地LinearLayout只能减缓

UI效率,其他毫无用处可言。

该复用性布局利用.xml呈现如下:

  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="horizontal">
  7. <Button
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="@string/add"/>
  11. <Button
  12. android:layout_width="fill_parent"
  13. android:layout_height="wrap_content"
  14. android:text="@string/delete"/>
  15. </LinearLayout>

为了避免冗余的布局元素,你可以使用<merge />作为复用性布局文件地root View 。例如:
           使用<merge />标签的布局文件:

  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 />节点并且直接添加两个Button去

取代<include />节点。

另外的,按需加载View视图 ,请看:

http://developer.android.com/training/improving-layouts/loading-ondemand.html

如何使ListView流畅滑动 ,请看:

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

Android中View绘制优化二一---- 使用<include />标签复用布局文件的更多相关文章

  1. Android中View绘制优化之一---- 优化布局层次

    本文原创, 转载请注明出处:http://blog.csdn.net/qinjuning 前言,竟然是翻译,当然得弄的有板有眼. 照着大作家格式来咯 , - - . 译序 最近一直在做锁屏界面,之前也 ...

  2. Android中View绘制优化

    1.优化布局层次 http://www.2cto.com/kf/201209/154108.html 2.使用<include />标签复用布局文件 http://www.2cto.com ...

  3. Android优化——UI优化(二) 使用include标签复用布局

    使用include标签复用布局 - 1.include标签的作用 假如说我下图的这个布局在很多界面都用到了,我该怎么办?每个页面都写一遍的话,代码太冗余,并且维护难度加大. <LinearLay ...

  4. Android中View绘制优化之三---- 优化View

    本文原创, 转载请注明出处:http://blog.csdn.net/qinjuning 译三: 优化视图 关于如何设计自定义View以及响应触摸时间等,请看Android developer : 地 ...

  5. Android中View绘制流程以及invalidate()等相关方法分析

    [原文]http://blog.csdn.net/qinjuning 整个View树的绘图流程是在ViewRoot.java类的performTraversals()函数展开的,该函数做的执行过程可简 ...

  6. Android中View绘制流程以及invalidate()等相关方法分析(转载的文章,出处在正文已表明)

    转载请注明出处:http://blog.csdn.net/qinjuning 前言: 本文是我读<Android内核剖析>第13章----View工作原理总结而成的,在此膜拜下作者 .同时 ...

  7. Android中View绘制流程以及invalidate()等相关方法分析(转)

    转自:http://blog.csdn.net/qinjuning 前言: 本文是我读<Android内核剖析>第13章----View工作原理总结而成的,在此膜拜下作者 .同时真挚地向渴 ...

  8. android 中View的优化

    在android开发中Listview是一个很重要的组件,它以列表的形式根据数据的长自适应展示具体内容,用户可以自由的定义listview每一列的布局,但当listview有大量的数据需要加载的时候, ...

  9. 【转载】Android 中 View 绘制流程分析

    创建Window 在Activity的attach方法中通过调用PolicyManager.makeNewWindo创建Window,将一个View add到WindowManager时,Window ...

随机推荐

  1. DZNEmptyDataSet框架简介

    给大家推荐一个设置页面加载失败时显示加载失败等的框架. 下载地址:DZNEmptyDataSet https://github.com/dzenbot/DZNEmptyDataSet 上效果首先在你的 ...

  2. 4.5 HOOK分发函数

    4.5 HOOK分发函数 本节开始深入的探讨键盘的过滤与反过滤.有趣的是,无论是过滤还是反过 滤,其原理都是进行过滤.取胜的关键在于:谁将第一个得到信息. 黑客可能会通过修改一个已经存在的驱动对象(比 ...

  3. .htaccess文件设置

    1.为使.htaccess有效,需要先做httpd.conf设置 #打开改写模式Options FollowSymLinks AllowOverride All#打开重写模式,去掉这行注释LoadMo ...

  4. js window.onload事件

    1.最简单的调用方式 直接写到html的body标签里面,如: ? 1 2 3 4     <html>       <body onload="func()"& ...

  5. 【集训笔记】归纳与递推【HDOJ1297

    例:(2050)折线分割平面 问题描述: 平面上有n条折线,问这些折线最多能将平面分割成多少块? 样例输入 1 2 样例输出 2 7 平面上有n条折线,问这些折线最多能将平面分割成多少块? 解: 折线 ...

  6. 【转】opencv-在图像上显示字符(不包括中文)

    原文参见:http://blog.csdn.net/ycc892009/article/details/6516528 1 #include <cv.h> #include <hig ...

  7. WTL---WxWidget---MFC 何去何从

    C++程序员打交道最多的就是MFC了,这个我不想多说,说来都是泪(C#年年更新,C++十年才出了一个featurePack还不是很好用) 现在另外两支队伍越来越庞大(所谓穷则思变,呵呵),一是WTL, ...

  8. jsp页面格式化数字或时间

    Tags   fmt:requestEncoding fmt:setLocale fmt:timeZone fmt:setTimeZone fmt:bundle fmt:setBundle fmt:m ...

  9. Android项目实战手机安全卫士(01)

    目录 项目结构图 源代码 运行结果 项目结构图 源代码 SplashActivity.java package com.coderdream.mobilesafe.activity; import a ...

  10. UIView详解1

    一个UIView的实例就是一个视图,表示的是屏幕上的一块矩形区域,负责这块矩形区域的描绘以及和用户的交互. 第一.UIView的可视化属性 1. backgroundColor  背景属性 2. hi ...