android merge的作用,Android的<merge>标签在XML布局中的用途是什么?

包含标签

该标签可让您将布局划分为多个文件:它可以帮助处理复杂的或超长用户界面。

假设您使用两个包含文件来分割复杂的布局,如下所示:

top_level_activity.xml:

android:id="@+id/layout1"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

然后,您需要编写include1.xml和include2.xml。

请记住,包含文件中的xml只是在渲染时转储到top_level_activity布局中(非常类似于#INCLUDEC 的宏)。

包含文件是纯简布局xml。

include1.xml:

android:id="@+id/textView1"

android:text="First include"

android:textAppearance="?android:attr/textAppearanceMedium"/>

...和include2.xml:

android:id="@+id/button1"

android:text="Button" />

看到?没有什么花哨。请注意,您仍然必须使用声明android名称空间xmlns:android="http://schemas.android.com/apk/res/android。

因此,top_level_activity.xml的呈现版本 为:

android:id="@+id/layout1"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

android:id="@+id/textView1"

android:text="First include"

android:textAppearance="?android:attr/textAppearanceMedium"/>

android:id="@+id/button1"

android:text="Button" />

在您的Java代码中,所有这些都是透明的:findViewById(R.id.textView1)在您的活动类中,返回正确的小部件(即使该小部件是在与活动布局不同的xml文件中声明的)。

最重要的是:可视化编辑器可以轻松处理事情。顶层布局呈现与包含的XML。

情节变厚

由于包含文件是经典布局xml文件,因此它必须具有一个top元素。因此,如果您的文件需要包含多个控件,则必须使用一种布局。

假设include1.xml现在有两个TextView:必须声明一个布局。让我们选择一个LinearLayout。

include1.xml:

android:id="@+id/layout2"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

android:id="@+id/textView1"

android:text="Second include"

android:textAppearance="?android:attr/textAppearanceMedium"/>

android:id="@+id/textView2"

android:text="More text"

android:textAppearance="?android:attr/textAppearanceMedium"/>

该top_level_activity.xml将呈现为:

android:id="@+id/layout1"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

android:id="@+id/layout2"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

android:id="@+id/textView1"

android:text="Second include"

android:textAppearance="?android:attr/textAppearanceMedium"/>

android:id="@+id/textView2"

android:text="More text"

android:textAppearance="?android:attr/textAppearanceMedium"/>

android:id="@+id/button1"

android:text="Button" />

但是等两个级别LinearLayout都是多余的!

确实,嵌套的两个LinearLayout没有任何作用,因为TextView可以layout1为完全相同的呈现将两个嵌套在下面。

所以,我们能做些什么?

输入合并标签

该标签只是一个虚拟标签,它提供一个顶级元素来处理这种冗余问题。

现在include1.xml变为:

android:id="@+id/textView1"

android:text="Second include"

android:textAppearance="?android:attr/textAppearanceMedium"/>

android:id="@+id/textView2"

android:text="More text"

android:textAppearance="?android:attr/textAppearanceMedium"/>

现在top_level_activity.xml呈现为:

android:id="@+id/layout1"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

android:id="@+id/textView1"

android:text="Second include"

android:textAppearance="?android:attr/textAppearanceMedium"/>

android:id="@+id/textView2"

android:text="More text"

android:textAppearance="?android:attr/textAppearanceMedium"/>

android:id="@+id/button1"

android:text="Button" />

您保存了一个层次结构级别,避免了一个无用的观点:Romain Guy已经睡得更好了。

你现在不快乐吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值