【问题标题】:How does <merge> tag work internally<merge> 标签如何在内部工作
【发布时间】:2015-05-07 11:12:21
【问题描述】:

我试图了解&lt;merge&gt; 标签在内部是如何工作的。我使用View Hierarchy 工具研究了一些示例。所以我了解基本使用以及它如何在更高级别上工作,但我想更多地研究这个标签和一般的膨胀视图。
所以让我们考虑一些简单的布局
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg"
    android:gravity="center_horizontal">

<include layout="@layout/titlebar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />

<TextView android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/some_text"
    android:text="@string/hello"
    android:padding="10dp" />

</RelativeLayout>

当然还有 titlebar.xml

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

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/chrome" />
</merge>

在这种情况下,我们有以下结果。

让我们改变我们的 titlebar.xml

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/some_text"
    android:src="@drawable/chrome" />

结果真的来了。

让我们在 activity_main.xml 文件中做更多的更改。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg"
    android:gravity="center_horizontal">

<include layout="@layout/titlebar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />

<TextView android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/bug"
    android:text="@string/hello"
    android:padding="10dp" />


    </LinearLayout>

而在 titlebar.xml 中,我保持一切不变。

所以在这种情况下,我们有一些奇怪的事情
1. 现在我们的根布局是LinearLayout。我们在标题栏文件中指定了不存在的属性android:layout_below="@+id/some_text"

  1. 在同一行中,我们有另一个问题@+id/some_text 不再存在,现在我们有@+id/bug。 我们来看看结果

这里我有一些问题:

  1. 我们可以在合并文件中使用任何属性(用于任何布局),但是如果包含此部分的视图组(布局)(在我们的例子中是 activity_main.xml)没有这样的属性,怎么办这种情况已经解决,只是我们在结果中看到的那样被忽略了吗?

  2. 我们在 imageview 上方硬编码了视图的 id,如果 id 存在,它就可以工作,但如果它不存在,正如我们在结果中看到的那样,它也被忽略了

所以我已经写了很多。 综上所述,XML解析器的工作与merge和include标签完全一致,也最好知道XML解析器的来源在哪里,看看它。

我将感谢所有阅读此行并可以提出建议或建议的人。

提前致谢。

【问题讨论】:

标签: android xml android-layout merge


【解决方案1】:

如果&lt;include&gt; 标记同时包含layout_width 和layout_height,它将覆盖所有根视图(它包含的文件的)布局参数。

merge 是一种避免视图层次结构增加深度的方法 - XML 布局文件必须只有一个根 - 所以它必须要么有一个 View,要么有一个 ViewGroup,它可以包含额外的 @ 987654325@s 或&lt;merge&gt;。当您在 ViewGroup 中包含具有多个 Views 的布局时,您可能会为您的层次结构添加额外的不必要的复杂性 - 例如,可能不需要 2 个垂直方向的 LinearLayouts。 merge 允许您删除多余的ViewGroup,并将其Views 合并到包含它的ViewGroup 中。

将带有merge 的布局包含在单个View 中与仅包含带有单个View 的布局相同,除了merge 没有“根视图”布局,所以include 不会覆盖它的布局参数(我相信)。

当View 或ViewGroup 膨胀为不支持子级指定布局参数的ViewGroup 时,这些参数将被删除。包含或膨胀(使用布局膨胀器)时会发生这种情况。

如果将子 View 或 ViewGroup 添加到 ViewGroup(使用 addView),则可能是子已经分配了布局参数,并且如果它们与 ViewGroup 不兼容正在添加,它可能会导致稍后在测量/布局期间发生类转换异常。

您的第二个问题与RelativeLayouts 布局规则有关,其行为可以使用android:layout_alignWithParentIfMissing 定义

【讨论】:

    猜你喜欢
    • 2012-05-09
    • 2011-04-28
    • 1970-01-01
    • 2011-08-06
    • 1970-01-01
    • 2019-01-13
    • 2010-10-14
    • 2013-09-28
    • 2012-08-01
    相关资源
    最近更新 更多