Android在layout xml中使用include[转]
在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[转]的更多相关文章
- Android在layout xml中使用include
Android include与merge标签使用详解 - shuqiaoniu的博客 - 博客频道 - CSDN.NEThttp://blog.csdn.net/shuqiaoniu/article ...
- Android在layout xml中使用include完成静态加载
Android在layout xml中使用include完成静态加载 include静态加载:不仅可以加载布局,还可以加载控件(控件标签名要在最外层)include标签中有个layout属性就是专门用 ...
- Android在layout xml中使用ViewStub完成动态加载
Android在layout xml中使用ViewStub完成动态加载 一.Layout XML文件常见的两种模块加载方式 1.静态加载:被加载的模块和其它模块加载的时间一样. <include ...
- android xml中使用include标签
在一个项目中,我们可能会在xml中局部用到相同的布局,如果每次都在xml中重写这些布局,代码显得很冗余.重复的复制黏贴也很烦恼,所以,我们把这些相同的局部布局写成一个单独的xml模块,需要用到这些布局 ...
- Android——inflate 将一个xml中定义的布局找出来
通俗的说,inflate就相当于将一个xml中定义的布局找出来. 因为在一个Activity里如果直接用findViewById()的话,对应的是setConentView()的那个layout里的组 ...
- 【原创】Android多个xml文件的使用
Android中经常会使用多个xml文件,但在Mainactivity中使用的setContentView(R.layout.main)只加载main.xml文件,其他xml文件不加载进当前视图,当我 ...
- Android XML中引用自定义内部类view的四个why
今天碰到了在XML中应用以内部类形式定义的自定义view,结果遇到了一些坑.虽然通过看了一些前辈写的文章解决了这个问题,但是我看到的几篇都没有完整说清楚why,于是决定做这个总结. 使用自定义内部类v ...
- 【转】在Android布局中使用include和merge标签
内容转自:http://fengweipeng1208.blog.163.com/blog/static/21277318020138229754135/ 在我们开发android布局时,经常会有很多 ...
- Android自己定义控件而且使其能够在xml中自己定义属性
为什么要自己定义View android开发中自己定义View的优点是显而易见的.比方说以下的这个顶部导航,它被设计出如今应用的每一个界面,但每次的内容却不尽同样.我们不能在每一个layout资源中都 ...
随机推荐
- Becoming inspired (2) - ASC 2017 March 25
Becoming inspired - part 2 @ Advanced Studio Classroom Vol: 2017 MARCH 25 7.Who was I like as a chil ...
- layoutSubviews相关总结
ios layout机制相关方法 - (CGSize)sizeThatFits:(CGSize)size - (void)sizeToFit ------- - (void)layoutSubview ...
- MS-SQL
变量 一个@为局部变量,两个@@为全局变量 @@error 最后一句SQL语句的错误编号 错误码 @@identity最后一次插入的标示值符 insert into biao(lie) output ...
- mongodb first
use [database] 使用数据库,新增文档后,数据库被自动创建 show dbs 显示所有数据库 db.[document].insert() 插入数据库 例:db.persons.inser ...
- oracle 网络环境配置
PLSQL Developer连接Oracle11g 64位数据库配置详解 最近换了台64bit的电脑,所以oracle数据库也跟着换成了64bit的,不过 问题也随之产生,由于plsql devel ...
- kubectl 获取信息
获取pod所在节点的ip kubectlget po tiller-deploy-8694f8fddc-c2rql -n kube-system -o jsonpath='{.status.hostI ...
- 关于时间查询的sql语句
今天 select * from 表名 where to_days(时间字段名) = to_days(now()); 昨天 SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ...
- Spring WebMVC 4.1返回json时 406(Not Acceptable)
1.问题现象Tomcat7+Spring4.1.4,返回json字符串时发生406错误 The resource identified by this request is only capable ...
- eclispe 通过git向码云上传
本文将介绍如何将本地的项目提交到开源中国上去,过程比较详细,实现起来很简单.由于自己也算是一个新手,所以没有做过多的解释,只是单纯的描述了该如何去做. 1.在开源中国上面新建一个空项目 到这里也就结束 ...
- pip & Jinja2
[pip&Jinja2] 2.7.9 以及上版本的 python 安装程序会自动安装 pip. 升级:pip install --upgrade pip. 使用pip安装Jinja2:sudo ...