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上找到了更好的解决方法: 解答道:必须同时重载layoutwidth和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

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

ref:

Android在layout xml中使用include
http://blog.csdn.net/race604/article/details/7564088

Android里merge和include标签的使用 - xiabo852105的专栏 - 博客频道 - CSDN.NET
http://blog.csdn.net/xiabo851205/article/details/7841937

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

时间: 2024-08-04 05:25:58

Android在layout xml中使用include的相关文章

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

Android在layout xml中使用include完成静态加载 include静态加载:不仅可以加载布局,还可以加载控件(控件标签名要在最外层)include标签中有个layout属性就是专门用来加载的. 在Android的layout样式定义中,可以使用xml文件方便的实现,有时候为了模块的复用,使用include标签可以达到此目的.例如: <include layout="@layout/otherlayout"></div> android开发的官方

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

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

通俗的说,inflate就相当于将一个xml中定义的布局找出来. 因为在一个Activity里如果直接用findViewById()的话,对应的是setConentView()的那个layout里的组件. 因此如果你的Activity里如果用到别的layout,比如对话框上的layout,你还要设置对话框上的layout里的组件(像图片 ImageView,文字TextView)上的内容,你就必须用inflate()先将对话框上的layout找出来,然后再用这个layout对象去 找到它上面的组

Android在Layout配置文件中配置颜色相关属性遇到的问题

今天朋友碰到一个问题,他的Android程序一启动就报错,Logcat报错有一个Fragment的Layout布局文件某一行解析错误. 是一个RadioGroup 其中的 RadioButton 设置了textColor属性,问题就在这里,textColor设置为一个选择器,但是这个选择器放在了drawable里面. 问题来了,textColor属性需要的是一个color 当一个选择器selector放置在drawable中的时候,解析时会按照图像进行解析. 解决问题方式: 在res文件夹中创建

android 读取 AndroidManifest.xml 中的数据:版本号、应用名称、自定义K-V数据(meta-data)

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android_metadata" android:versionCode="1" android:versionName="

Android自动化编译设置AndroidManifest.xml中package值(包名)

手动修改Android的AndroidManifest.xml中package值(包名)很简单,手动修改即可.但是项目中需要把Android的项目源代码放到服务器端在客户下载时候动态编译生成,且生成的app签名相同但包名不同(若此时包名相同就是相同的app),这种需求需要在服务器端自动化编译Android源代码时候自动调整AndroidManifest.xml中package值才能实现.查阅相关资料,结合Java对XML文件的读写技术,写了一个方法,让服务器端在编译之前预加载修改AndroidM

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

Android中经常会使用多个xml文件,但在Mainactivity中使用的setContentView(R.layout.main)只加载main.xml文件,其他xml文件不加载进当前视图,当我们要用到其他xml文件中的控件是发现直接使用findViewById()方法时不报错但控件的值找不到为null,而一旦为该控件添加相应事件就会出现空指针异常.原因就在于控件并未加载进当前视图. 解决方法:两种 1.使用在main.xml中使用include语句 <include layout="

Android中layout.xml文件中加载自定义的View类

<com.bn.summer.GGView3 android:layout_width="100dip" android:layout_height="114dip" android:layout_marginLeft="11dip" /> View类的实现: package com.bn.summer; import android.content.Context; import android.content.res.Resour

Android Layout Binder(在线将XML中View find出来,生成java代码的工具)

废话不多说,这是地址:http://android.lineten.net/layout.php. 有图有真相,例如: 你的XML假如是这样: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fil