内容转自:http://fengweipeng1208.blog.163.com/blog/static/21277318020138229754135/

在我们开发android布局时,经常会有很多的布局是相同的,这个时候我们可以通过<include/>和<merge/>标签实现将复杂的布局包含在需要的布局中,减少重复代码的编写。

1. 创建一个可以重复使用的布局:

如下代码描述在应用中每个acitivity都出现的顶栏titlebar.xml

 <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:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/gafricalogo" />
</FrameLayout>

上面的根布局(root view)即frameLayout会出现在之后插入的地方。

2. 使用<include/>标签:

在应用中的一个activity的布局中顶栏就是如上的布局,那么我们就可以include上面的titlebar.xml达到复用的效果,布局代码如下:

 <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 android:id="@+id/new_title"
layout="@layout/titlebar"/> <TextView android:layout_width=”match_parent”
android:layout_height="wrap_content"
android:text="@string/hello"
android:padding="10dp" /> ... </LinearLayout>

通过取得元素的id,我们可以修改include标签中元素的属性,下面的例子为在actiivty中修改titlebar中的图片:

 private View mTitleBar = null;
private ImageView mTitleImageView = null; mTitleBar = findViewById(R.id.new_title);
mTitleImageView = (ImageView)mTitleBar.findViewById(R.id.title);
mTitleImageView.setImageResource(R.drawable.logo);

在使用include标签时,我们可以覆写插入布局root view的属性(所有的android:layout_*属性):

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

如果需要覆写插入布局root view的属性,则必须制定android:layout_width和android:layout_height这两个属性以使其它的覆写属性生效。

注意:被引用的布局的属性中,只有,外层的layout属性可以被修改,内部属性不能被修改。

举例:button2.xml,内容如下:

 <Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
那么在别的地方引用这个布局:
 
        <include
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

android:text="Button23456"

            layout="@layout/button2" />
 

那么,绿色的代码设置的属性是有效的,红色的代码设置的属性是无效的。

其实,这很容易理解,就是include是为了复用一段封装好的布局,那么,布局内部的东西自然是不用改的,如果要改,还不如在被引用的文件中改好,我们在include中修改的只是被引用的布局的大小位置。

3. 使用<merge/>标签

merge标签用来消除我们在include一个布局到另一个布局时所产生的冗余view group。比如现在很多布局中会有两个连续的Button,于是我们将这两个连续的Button做成可复用布局(re-usable layout)。在使用include标签时我们必须先将这两个Button用一个view group比如LinearLayout组织在一起然后供其它布局使用,如果是include的地方也是LiearLayout就会造成有两层连续的LiearLayout,除了降低UI性能没有任何好处。这个时候我们就可以使用<merge/>标签作为可复用布局的root view来避免这个问题。

 <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>

当我们用<include/>标签复用上述代码时,系统会忽略merge元素,直接将两个连续的Button放在<include/>标签所在处。

【转】在Android布局中使用include和merge标签的更多相关文章

  1. android布局中使用include及需注意点

    在android布局中,使用include,将另一个xml文件引入,可作为布局的一部分,但在使用include时,需注意以下问题: 一.使用include引入 如现有标题栏布局block_header ...

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

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

  3. Android布局优化:include 、merge、ViewStub的详细总结

    版权声明:本文出自汪磊的博客,未经作者允许禁止转载. 本篇博客主要是对上篇博客的补充Android性能优化之UI渲染性能优化, 没有什么新东西,觉得应该是都掌握的玩意,写出来也只是自己做个小小的总结. ...

  4. Android UI 优化 使用<include/>和 <merge />标签

    使用<include /> 标签来重用layout代码 如果在一个项目中需要用到相同的布局设计,可以通过<include /> 标签来重用layout代码,该标签在Androi ...

  5. Android的布局优化之include、merge 、viewstub

    以前在写布局的时候总是喜欢用自己熟悉的方式去写,从来也没有想过优化怎么的,后来又一次在上班的时候老大拿着我写的一个页面说我这个不行.我说这不是和设计图上的一模一样的么?怎么就不行了?然后他就跟我说了一 ...

  6. Android布局中的空格以及占一个汉字宽度的空格的实现

    在Android布局中进行使用到空格,以便实现文字的对齐.那么在Android中如何表示一个空格呢? 空格:  窄空格:  一个汉字宽度的空格:   [用两个空格(  )占一个汉字的宽度时,两个空格比 ...

  7. Android布局中的空格以及占一个汉字宽度的空格,实现不同汉字字数对齐

    前言 在Android布局中进行使用到空格,以便实现文字的对齐.那么在Android中如何表示一个空格呢? 空格: (普通的英文半角空格但不换行) 窄空格:   (中文全角空格 (一个中文宽度))   ...

  8. Android 布局中 如何使控件居中

    首先要分两种不同情况,在两种不同的布局方式下:LinearLayout 和RelativeLayout 1. LinearLayout a). android:layout_gravity=" ...

  9. Android布局中涉及的一些属性

    Android:gravity属性 线性布局常见的就是利用LinearLayout进行布局,其中有个比较重要的属性就是android:gravity,在官方文档中是这么描述这个属性的:指定一个元素怎么 ...

随机推荐

  1. 不简单的SQL查询和排序语句

    真不简单!! 一:使用select语句进行查询 语法: SELECT    <列名> FROM      <表名> [WHERE    <查询条件表达式>] [OR ...

  2. (转)B-树、B+树、B*树

    B-树 是一种多路搜索树(并不是二叉的): 1.定义任意非叶子结点最多只有M个儿子:且M>2: 2.根结点的儿子数为[2, M]: 3.除根结点以外的非叶子结点的儿子数为[M/2, M]: 4. ...

  3. 【精心推荐】几款实用的 JavaScript 图形图表库

    一款好的图表插件不是那么容易找到的.最近项目里需要实现统计图表功能,所以在网上搜罗了一圈,找到一些不错的图表插件,分享大家.众多周知,图形和图表要比文本更具表现力和说服力.这里给大家精心推荐几款实用的 ...

  4. Spring(2) ------ 依赖注入

    spring框架为我们提供了三种注入方式,分别是set注入,构造方法注入,接口注入. 1.set注入: 采用属性的set方法进行初始化,就成为set注入. 1)给普通字符类型赋值. public cl ...

  5. CSS3动画(个人理解)

    随着学习的深入,越来越觉得Css3动画的重要,虽然JQ自定义动画和动画回调函数必须掌握,但是css3动画做起来更加绚丽,更加方便!1.常规使用1.1 使用transition属性,一般我们是配合hov ...

  6. 最简单的tab切换

    JS: $(".con").eq(0).show();    $(".btn span").click(function(){        var num = ...

  7. ABAP绘图功能模块概观(转)

    ABAP Graphics FM OverviewABAP绘图功能模块概观 此处仅将功能模块及范例程序列出(若要列出详细参数篇幅过大) 2 Main Graphics Demo Program: GR ...

  8. -[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object' 解决方法

    -[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object' 解决方法: 错误:NSMutableD ...

  9. Android 手机卫士--九宫格使用

    本文地址:http://www.cnblogs.com/wuyudong/p/5907736.html,转载请注明源地址. 采用GridView来实现,和ListView使用方式类似,列数(3列) 首 ...

  10. 【代码笔记】iOS-判断有无网络

    一,工程图. 二,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController : UIVi ...