Re-using Layouts with <include/>

  Although Android offers a variety of widgets to provide small and re-usable interactive elements, you might also need to re-use larger components that require a special layout. To efficiently re-use complete layouts, you can use the <include/> and <merge/> tags to embed another layout inside the current layout.

  Reusing layouts is particularly powerful as it allows you create reusable complex layouts. For example, a yes/no button panel, or custom progress bar with description text. It also means that any elements of your application that are common across multiple layouts can be extracted, managed separately, then included in each layout. So while you can create individual UI components by writing a custom View, you can do it even more easily by re-using a layout file.

可以在一个layout中使用 <include/> and <merge/>  嵌入其它layout

Create a Re-usable Layout


  If you already know the layout that you want to re-use, create a new XML file and define the layout. For example, here's a layout from the G-Kenya codelab that defines a title bar to be included in each activity (titlebar.xml):

下面是一个可复用的layout,它将要被其它layout include
 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width=”match_parent”
android:layout_height="wrap_content"
android:background="@color/titlebar_bg"> <ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/gafricalogo" />
</FrameLayout>

  The root View should be exactly how you'd like it to appear in each layout to which you add this layout.

Use the <include> Tag


  Inside the layout to which you want to add the re-usable component, add the <include/> tag. For example, here's a layout from the G-Kenya codelab that includes the title bar from above:

<include>示例

Here's the layout file:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:background="@color/app_bg"
android:gravity="center_horizontal"> <include layout="@layout/titlebar"/> <TextView android:layout_width=”match_parent”
android:layout_height="wrap_content"
android:text="@string/hello"
android:padding="10dp" /> ... </LinearLayout> 

  You can also override all the layout parameters (any android:layout_* attributes) of the included layout's root view by specifying them in the <include/> tag.

可以在 <include>中使用 android:layout_* 系列属性会重写被include的layout的根标签的同名属性,一但果重写其中一个,就必需要重写 android:layout_heightand android:layout_width

For example:

 <include android:id=”@+id/news_title”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
layout=”@layout/title”/>

  However, if you want to override layout attributes using the <include> tag, you must override both android:layout_height and android:layout_width in order for other layout attributes to take effect.

Use the <merge> Tag


  The <merge /> tag helps eliminate redundant view groups in your view hierarchy when including one layout within another. For example, if your main layout is a vertical LinearLayout in which two consecutive views can be re-used in multiple layouts, then the re-usable layout in which you place the two views requires its own root view. However, using another LinearLayout as the root for the re-usable layout would result in a vertical LinearLayout inside a vertical LinearLayout. The nested LinearLayout serves no real purpose other than to slow down your UI performance.

  当一个layout include 另一个layout时,<merge>用来去除重复的布局标签.
举例:两个layout A和B,其中A是垂直LinearLayout且include B,如果B也是垂直LinearLayout,那么就会在A的LinearLayout中嵌入另一个同样的Layout,
没有意义,反而会减慢速度。这时可以把B的根标签写成 <merge xmlns:android="http://schemas.android.com/apk/res/android" >
注意 <merge>只能作根标签,且系统会忽视它。

  To avoid including such a redundant view group, you can instead use the <merge> element as the root view for the re-usable layout. For example:

 <merge xmlns:android="http://schemas.android.com/apk/res/android">

     <Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/add"/> <Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/delete"/> </merge>

  Now, when you include this layout in another layout (using the <include/> tag), the system ignores the <merge> element and places the two buttons directly in the layout, in place of the <include/> tag.

Layout Resource官方教程(4)<include>与<merge>的更多相关文章

  1. Layout Resource官方教程(3)在layout中用include嵌入其它layout

    简介 <include>Includes a layout file into this layout. 类似 #include ,把layout展开在include处 attribute ...

  2. Layout Resource官方教程(1)简介

    Layout Resource SEE ALSO Layouts A layout resource defines the architecture for the UI in an Activit ...

  3. Layout Resource官方教程(2)用ViewStub引用的嵌入的layout可推迟加载

    Loading Views On Demand THIS LESSON TEACHES YOU TO Define a ViewStub Load the ViewStub Layout YOU SH ...

  4. android学习——Android Layout标签之-viewStub,requestFocus,merge,include

    定义Android Layout(XML)时,有四个比较特别的标签是非常重要的,其中有三个是与资源复用有关,分别是<viewStub/>, <requestFocus />, ...

  5. Android布局优化之ViewStub、include、merge使用与源码分析

    在开发中UI布局是我们都会遇到的问题,随着UI越来越多,布局的重复性.复杂度也会随之增长.Android官方给了几个优化的方法,但是网络上的资料基本上都是对官方资料的翻译,这些资料都特别的简单,经常会 ...

  6. include、merge 、ViewStub

    在布局优化中,Androi的官方提到了这三种布局<include />.<merge />.<ViewStub />,并介绍了这三种布局各有的优势,下面也是简单说一 ...

  7. Android布局优化之include、merge、ViewStub的使用

    本文针对include.merge.ViewStub三个标签如何在布局复用.有效减少布局层级以及如何可以按需加载三个方面进行介绍的. 复用布局可以帮助我们创建一些可以重复使用的复杂布局.这种方式也意味 ...

  8. Android 布局巧用之include、merge、ViewStub

    原文链接:https://mp.weixin.qq.com/s/bTA2gztUzqvqER2rz56RRQ 相信大家经常听到include.merge.ViewStub这样的标签,官方也提到这三种布 ...

  9. Android UI优化——include、merge 、ViewStub

    在布局优化中,Androi的官方提到了这三种布局<include />.<merge />.<ViewStub />,并介绍了这三种布局各有的优势,下面也是简单说一 ...

随机推荐

  1. ▲▲▲▲▲▲▲▲▲▲▲yum源的配置(本地和ftp)▲▲▲▲▲▲▲▲▲▲▲▲▲v

    ★★★★★★★★★★★★★★★本机yum源★★★★★★★★★★★★★★★★ 1. 首先把DVD里的OS镜像mount处理,如果插入光驱自动mount的话,一般在/media下面,比如RHEL_6.3 ...

  2. 解决Ajax跨域问题:Origin xx is not allowed by Access-Control-Allow-Origin.

    一:使用jsonp格式, 如jquery中ajax请求参数   dataType:'JSONP'. <html> <head> <title>title</t ...

  3. thinkphp操作数据库

    1.实现or操作: $where=array( 'city'=>array('like',array('%'.$_GET['city'].'%')); 'hangye'=>array('l ...

  4. Natural Language Processing with Python - Chapter 0

    一年之前,我做梦也想不到会来这里写技术总结.误打误撞来到了上海西南某高校,成为了文科专业的工科男,现在每天除了膜ha,就是恶补CS.导师是做计算语言学的,所以当务之急就是先自学计算机自然语言处理,打好 ...

  5. FatMouse

      时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:1431 解决:641 题目描述: FatMouse prepared M pounds of cat food, ready to t ...

  6. 2016 Multi-University Training Contest 1 Necklace 环排+二分匹配

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5727 题意:由2*N颗宝石构成的环(阴阳宝石均为N颗且标号均从1~N) 之后给定M组 a,b;表示阳宝石a ...

  7. 【转】perl 变量 $/ 的用法解析 上下文为行模式时,$/ 定义以什么来区分行

    默认状态下,很显然都是用\n来区分行,\n也被我们称作为换行符. 当读取序列时,按行来读取时,就是以换行符为标准. 读取的strawberry1.gb的文件内容如下: LOCUS JX118024 4 ...

  8. win 7 64位如何安装erdas 9.2

    最主要的就是crack包必须包含这三个文件:erdas.exe、license.dat和lmgrd.exe 将这三个文件都复制到C盘安装目录下bin中,其余安装同win 7 32位系统

  9. Android中9patch图片格式(xx.9.png)介绍与制作详解

    一:9patch图片介绍: android的.9.png是android系统中一种特殊的图片格式,专门用来用来处理图片大小变化后(如拉伸)的失真,不正常,如我们看到的qq聊天中的文字气泡,不管你输入的 ...

  10. TWaver3D入门探索——3D拓扑图之绽放的小球花

    这样一簇绚烂丰满艳丽多姿的3D小球花,要多少代码才能完成?其实不足百行,您信吗?下面咱就看一下具体实现过程,让您分分钟学会用TWaver HTML5制作3D拓扑图. 搭建3D空间 首先为花簇的绽放建一 ...