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

<include layout="@layout/otherlayout"></div>

Android开发的官方网站的说明在这里
其中,有提到:

Similarly, you can override all the layout parameters. This means that any android:layout_* attribute can be used with the <include> tag.

意思是任何android:layout_*属性都可以应用在标签中。

如果使用如下代码:

<Relativelayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" > <Textview
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/somestring"
android:id="@+id/top" /> <include layout="@layout/otherlayout"
android:layout_below="@id/top" />
</Relativelayout >

发现include的otherlayout,并没有在如我们预期的在id/top这个TextView下面,而是忽略了android:layout_below属性。经过Google发现,很多人遇到类似的问题。

有解决方法是在include的外面再包一层LinearLayout,如下:

<Linearlayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/top" > <include layout="@layout/otherlayout">
</Linearlayout >

在Statckoverflow上找到了更好的解决方法:
解答道:必须同时重载layout_width和layoutheight熟悉,其他的layout*属性才会起作用,否这都会被忽略掉。上面的例子应该写成这样:

<include layout="@layout/otherlayout">
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_below="@id/top" />

另外,关于xml的复用,还可以使用merge标签,merge标签主要是用来优化显示的,减少View树的层级,可以参考这里:https://developer.android.com/resources/articles/layout-tricks-merge.html 翻译版在这里:http://apps.hi.baidu.com/share/detail/20871363

from:http://www.race604.com/using-include-in-layout/

Android在layout xml中使用include[转]的更多相关文章

  1. Android在layout xml中使用include

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

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

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

  3. Android在layout xml中使用ViewStub完成动态加载

    Android在layout xml中使用ViewStub完成动态加载 一.Layout XML文件常见的两种模块加载方式 1.静态加载:被加载的模块和其它模块加载的时间一样. <include ...

  4. android xml中使用include标签

    在一个项目中,我们可能会在xml中局部用到相同的布局,如果每次都在xml中重写这些布局,代码显得很冗余.重复的复制黏贴也很烦恼,所以,我们把这些相同的局部布局写成一个单独的xml模块,需要用到这些布局 ...

  5. Android——inflate 将一个xml中定义的布局找出来

    通俗的说,inflate就相当于将一个xml中定义的布局找出来. 因为在一个Activity里如果直接用findViewById()的话,对应的是setConentView()的那个layout里的组 ...

  6. 【原创】Android多个xml文件的使用

    Android中经常会使用多个xml文件,但在Mainactivity中使用的setContentView(R.layout.main)只加载main.xml文件,其他xml文件不加载进当前视图,当我 ...

  7. Android XML中引用自定义内部类view的四个why

    今天碰到了在XML中应用以内部类形式定义的自定义view,结果遇到了一些坑.虽然通过看了一些前辈写的文章解决了这个问题,但是我看到的几篇都没有完整说清楚why,于是决定做这个总结. 使用自定义内部类v ...

  8. 【转】在Android布局中使用include和merge标签

    内容转自:http://fengweipeng1208.blog.163.com/blog/static/21277318020138229754135/ 在我们开发android布局时,经常会有很多 ...

  9. Android自己定义控件而且使其能够在xml中自己定义属性

    为什么要自己定义View android开发中自己定义View的优点是显而易见的.比方说以下的这个顶部导航,它被设计出如今应用的每一个界面,但每次的内容却不尽同样.我们不能在每一个layout资源中都 ...

随机推荐

  1. MySQLReport

    简介: MySQLReport 一.安装 shell > yum -y install mysqlreport perl-DBD-MySQL 二.使用 shell > mysqlrepor ...

  2. Graylog安装配置

    ES集群健康检测:curl -sXGET http://localhost:9200/_cluster/health?pretty=true | grep "status" | a ...

  3. 输入N组父子对,求父子对所组成的二叉树的高度----17年某公司的笔试题

    题目的大致意思如下: 输入N组数,一组数代表一个父子对(如,0 1,0代表父节点,1代表子节点),求这N组数所组成的二叉树的高度: 例如: 输入:6  0 1  0 2  1 3  1 4  2 5 ...

  4. samtools软件的使用

    1)samtools简介--------------------------------------------------------------------------背景:前面我们讲过sam/b ...

  5. ubuntu部署jenkins

    https://www.cnblogs.com/lozz/p/9962316.html 1.安装 wget -q -O - https://pkg.jenkins.io/debian/jenkins- ...

  6. discrete

    discrete - 必应词典 美[dɪ'skrit]英[dɪ'skriːt] adj.离散的:分离的:各别的 网络不连续的:分立的:离散型

  7. 鼠标滑过图片添加边框图片无位移[xyytit]

    实现下面的效果,鼠标滑过图片添加边框图片无位移——鼠标滑过,图片只是加了边框,不会晃动: 参考代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML ...

  8. python之面向对象之反射运用

    先看下hasattr和getattr在反射中的用法 import sys class apache(object): def __init__(self,tcp): self.tcp = tcp de ...

  9. [leetcode]340. Longest Substring with At Most K Distinct Characters至多包含K种字符的最长子串

    Given a string, find the length of the longest substring T that contains at most k distinct characte ...

  10. Zabbix监控PostgreSQL

    目录 Zabbix监控PostgreSQL 1. 安装libzbxpgsql 2. 配置zabbix配置文件zabbix_agentd.conf 3. 创建监控用户 4. 导入监控模板 5. 主机链接 ...