Android layout 使用include和merge 标签


 使用<include /> 标签来重用layout代码

如果在一个项目中需要用到相同的布局设计,可以通过<include /> 标签来重用layout代码,该标签在android开发文档中没有相关的介绍。在android主屏程序中  用到了这个标签:

<com.android.launcher.Workspace
android:id="@+id/workspace"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
launcher:defaultScreen="1">
<include android:id="@+id/cell1" layout="@layout/workspace_screen" />
<include android:id="@+id/cell2" layout="@layout/workspace_screen" />
<include android:id="@+id/cell3" layout="@layout/workspace_screen" />

</com.android.launcher.Workspace>
这样可以多次引用一个布局片段而不用重复的复制、粘贴。通过include标签也可以覆写一些属性的值,例如上面的示例就覆写了引用的layout中的id值。下面是另外一个示例:
<include android:layout_width="fill_parent" layout="@layout/image_holder" />
<include android:layout_width="256dip" layout="@layout/image_holder" />
使用<merge /> 标签来减少视图层级结构 
在Android layout文件中需要一个顶级容器来容纳其他的组件,而不能直接放置多个组件,例如如下的代码

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

android:layout_width="fill_parent"

android:layout_height="fill_parent">

<ImageView

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:scaleType="center"

android:src="@drawable/golden_gate" />

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Golden Gate" />

</FrameLayout>

 

如果能在layout文件中把FrameLayout声明去掉就可以进一步优化布局代码了。 但是由于布局代码需要外层容器容纳,如果
直接删除FrameLayout则该文件就不是合法的布局文件。这种情况下就可以使用<merge /> 标签了。
修改为如下代码就可以消除多余的FrameLayout了:

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

<ImageView

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:scaleType="center"

android:src="@drawable/golden_gate" />

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Golden Gate" />

</merge>

merge也有一些使用限制: 只能用于xml layout文件的根元素;在代码中inflate一个以merge为根元素的
布局文件时候,你需要指定一个ViewGroup 作为其容器,并且要设置attachToRoot 为true,详细信息参考
inflate()函数doc
如果在一个项目中需要用到相同的布局设计,可以通过<include /> 标签来重用layout代码,该标签在android开发文档中没有相关的介绍。在android主屏程序中 用到了这个标签:

<com.android.launcher.Workspace
  android:id="@+id/workspace"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  launcher:defaultScreen="1">
  <include android:id="@+id/cell1" layout="@layout/workspace_screen" />
  <include android:id="@+id/cell2" layout="@layout/workspace_screen" />
  <include android:id="@+id/cell3" layout="@layout/workspace_screen" />

</com.android.launcher.Workspace>
这样可以多次引用一个布局片段而不用重复的复制、粘贴。通过include标签也可以覆写一些属性的值,例如上面的示例就覆写了引用的layout中的id值。下面是另外一个示例:
<include android:layout_width="fill_parent" layout="@layout/image_holder" />
<include android:layout_width="256dip" layout="@layout/image_holder" />

使用<merge /> 标签来减少视图层级结构

在Android layout文件中需要一个顶级容器来容纳其他的组件,而不能直接放置多个组件,例如如下的代码
:

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

    android:layout_width="fill_parent"

    android:layout_height="fill_parent">

    <ImageView

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:scaleType="center"

        android:src="@drawable/golden_gate" />

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Golden Gate" />

</FrameLayout>


上面的代码显示一个图片,然后在图片上方显示一个title, 结果如下图:

android activity的默认布局为FrameLayout,这样上面的布局代码就有2层FrameLayout,通过HierarchyViewer
工具看到的结构如下:

如果能在layout文件中把FrameLayout声明去掉就可以进一步优化布局代码了。 但是由于布局代码需要外层容器容纳,如果
直接删除FrameLayout则该文件就不是合法的布局文件。这种情况下就可以使用<merge /> 标签了。
修改为如下代码就可以消除多余的FrameLayout了:

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

    <ImageView

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:scaleType="center"

        android:src="@drawable/golden_gate" />

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Golden Gate" />

</merge>

通过HierarchyViewer工具看到的结构如下:

merge也有一些使用限制: 只能用于xml layout文件的根元素;在代码中inflate一个以merge为根元素的
布局文件时候,你需要指定一个ViewGroup 作为其容器,并且要设置attachToRoot 为true,详细信息参考
inflate()函数doc。

上面示例项目代码:
http://progx.org/users/Gfx/android/MergeLayout.zip
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值