转:http://4265337.blog.163.com/blog/static/195375820127935731114/

include和merge标记的作用主要是为了解决layout的重用问题。

比如我们有三四个Activity但是他们都要用到同一个样式的标题栏,虽然我们把一样的代码copy个三四遍也没关系,但实在是太丑了,而且效率太低,如果这个标题栏要改样式,你岂不是要去三四个地方分别改动。

为了解决这个问题,android中有了include和merge标记

以下为标题栏的layout文件titlebar.xml  我们将使用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="http://4265337.blog.163.com/blog/@drawable/gafricalogo"
/>
</FrameLayout>

那么在那三四个activity中你可以适用Include标记

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
android:orientation="vertical"
   
android:layout_width=”match_parent”
   
android:layout_height=”match_parent”
   
android:gravity="center_horizontal">

    <include
layout="@layout/titlebar"/>  

<TextView
android:layout_width=”match_parent”
             
android:layout_height="wrap_content"
             
android:text="@string/hello" />

...

</LinearLayout>

调用了Include之后,titlebar文件的内容就被完全嵌入到了include所指定的位置。而且你还可以在include中重新更改一些属性的值,比如

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

layout="@layout/title"/>
原来layout中的wrap_content属性就被改成了match_parent属性

再来说一下merge标记

上面的include有一个副作用就是他多套了一层root节点FrameLayout ,使得再构图的时候会多花费一点时间

如果你不能容忍这个的话那你可以试一下merge标记

titlebar2.xml

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

<ImageView
android:layout_width="wrap_content"
           
android:layout_height="wrap_content" 
            android:src="http://4265337.blog.163.com/blog/@drawable/gafricalogo"
/>

</merge>

这样行成的titlebar2文件就少了外层的root节点,merge标记可以直接成为root节点,当titlebar2被include到文件中时,merge标记就会被忽略掉,而直接由里面的ImageView取代原来include的位置。避免了冗余的layout。

所以include和merge是配合使用的,不是一个互斥的或者说是平级的关系。

再来说一个在使用这两个标签时最容易出现的问题。

经常会有同学在RelativeLayout中使用include标签

但是却发现include进来的控件无法用layout_alignParentBottom="true"之类的标签来调整。这个真的非常恼火。其实解决方法非常简单,只要你在include的时候同时重载下layout_width和layout_height这两个标签就可以了。如果不重载,任何针对include的layout调整都是无效的!

android include标签的使用,在RelativeLayout中使用include标签需注意!!!!!的更多相关文章

  1. JSP中动态include和静态include区别

    静态include(<%@ include file=""%>): 静态include(静态导入)是指将一个其他文件(一个jsp/html)嵌入到本页面 jsp的inc ...

  2. 如何自定义JSTL标签与SpringMVC 标签的属性中套JSTL标签报错的解决方法

    如何自定义JSTL标签 1.创建一个类,从SimpleTagSupport继承 A) 通过继承可以获得当前JSP页面上的对象,如JspContext I) 实际上可以强转为PageContext II ...

  3. mybatis中的一些标签使用

    主要有两个配置文件,一个是主配置文件SqlConfig.xml, 还有一个是dao接口实现类相对应的mapper的配置文件 .比如userDao的userDao.xml配置文件. 1.resultTy ...

  4. Struts2的标签库(一)——使用Struts2的标签库

    Struts2的标签库(一) --使用Struts2的标签库 1.Struts2的标签库其实就是一个自定义的标签库,所以它也有它的标签处理类和标签库定义文件: 2.和所有自定义标签一样,我们可以找到S ...

  5. Android在layout xml中使用include

    Android include与merge标签使用详解 - shuqiaoniu的博客 - 博客频道 - CSDN.NEThttp://blog.csdn.net/shuqiaoniu/article ...

  6. RelativeLayout中include 控件覆盖重叠的问题

    RelativeLayout直接include另一个layout是会把include中的控件与当前layout中的控件覆盖重叠,经过查资料 其中的include标签一定要加上(因为include中不指 ...

  7. Android在layout xml中使用include[转]

    在Android的layout样式定义中,可以使用xml文件方便的实现,有时候为了模块的复用,使用include标签可以达到此目的.例如: <include layout="@layo ...

  8. Android在layout xml中使用include完成静态加载

    Android在layout xml中使用include完成静态加载 include静态加载:不仅可以加载布局,还可以加载控件(控件标签名要在最外层)include标签中有个layout属性就是专门用 ...

  9. Android WebView的HTML中的select标签不起作用

    Android WebView的HTML中的select标签不起作用 经过查询资料,了解到android对html里的select标签是弹出一个原生的选择器. 问题: Webview中的select没 ...

随机推荐

  1. Win10尝鲜体验——初识传说中不一样的Windows 分类: 资源分享 2015-07-24 18:27 13人阅读 评论(0) 收藏

    这几天,网上传来一个消息,虽然不知是好是坏,Win10可以下载安装了! 出于好奇,下载尝鲜,几个截图,留作纪念~ 中文,还是要好好支持的,毕竟中国有如此多的用户 可选的安装版本 许可条款也刚刚出炉,估 ...

  2. Spring核心框架体系结构(jar包引用分析)[转]

    很多人都在用spring开发java项目,普通添加lib目录拷贝jar包,或者创建maven项目时,配置maven依赖的时候并不能明确要配置哪些spring的jar,经常是胡乱添加一堆,编译或运行报错 ...

  3. win7下安装双系统Ubuntu14.04后开机没有win7,直接进入Ubuntu

    开机进入Ubuntu后,打开命令端,输入: sudo update-grub 然后重启,则解决问题

  4. u3d DontDestroyOnLoad多场景问题

    using UnityEngine; using System.Collections; public class DontDel : MonoBehaviour { public GameObjec ...

  5. Windows下使用最新的JDK1.7.0_51以上版本连接Jenkins出现SecurityException

    我在slave节点上安装了jdk1.8, 当在节点上启动slave-agent的时候,报安全性限制的错误: java.lang.SecurityException: Missing required ...

  6. Bootstrap 轮播(Carousel)详解

    Bootstrap 轮播(Carousel)插件是一种灵活的响应式的向站点添加滑块的方式.除此之外,内容也是足够灵活的,可以是图像.内嵌框架.视频或者其他您想要放置的任何类型的内容.如果您想要单独引用 ...

  7. 关于HTML标签嵌套的问题详解

    先说基础,HTML标签有两类: 1.块级元素 div.h1~h6.address.blockquote.center.dir.dl.dt.dd.fieldset.form.hr.isindex.men ...

  8. Phpcms v9 实现首页|列表页|内容页调用点击量的代码

    很多朋友经常问Phpcms v9的首页.列表页.内容页点击量如何调用.今天给大家分享phpcms V9如何分别在首页.列表页.内容页调用点击量代码 1,Phpcms v9首页调用点击量 {pc:con ...

  9. C# toolstrip 上添加DateTimePicker Control控件

    private void AddDTPtoToolstrip( n){DateTimePicker dtp = DateTimePicker;dtp.Width = 110;dtp.Format = ...

  10. 关于 wsdl2Java 自动生成客户端调取webservice接口

    webservice地址:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl wsdl2Java 自动生成类名: 客户端调 ...