<merge> 标签如何在内部工作
How does <merge> tag work internally
我想了解 <merge>
标签的内部工作原理。我已经使用 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"
- 在同一行中我们有另一个问题
@+id/some_text
不再存在,现在我们有 @+id/bug
。
来看看结果
这里我有一些问题:
我们可以在合并文件中使用任何属性(用于任何布局),但是如果包含此部分的视图组(布局)(在我们的例子中 activity_main.xml)不有这样的属性,这种情况是如何解决的,正如我们在结果中看到的那样被忽略了?
我们在 imageview 上面硬编码了视图的 id,如果 id 存在,它就可以工作,但如果不存在,正如我们在结果中看到的那样,它也只是被忽略了
所以我已经写了很多了。
综上所述,XML 解析器的工作与合并和包含标记完全一致,了解 XML 解析器的源代码所在的位置,以浏览它也是很好的。
我将感谢所有阅读此行并提出建议或建议的人。
提前致谢。
如果 <include>
标签包含 layout_width 和 layout_height,它将覆盖所有根视图的(它包含的文件的)布局参数。
A merge
是一种避免在视图层次结构中增加深度的方法 - XML 布局文件必须只有一个根 - 所以它必须有一个 View
,一个 ViewGroup
,它可以包含额外的 View
或一个 <merge>
。当您在 ViewGroup
中包含具有多个 View
的布局时,您可能会向层次结构添加额外的不必要的复杂性 - 例如,可能不需要 2 个垂直方向的 LinearLayout
. merge
允许您删除多余的 ViewGroup
,并将其 View
合并到包含它的 ViewGroup
中。
包含带有 merge
和单个 View
的布局与直接包含带有单个 View
的布局相同,只是带有 merge
没有布局的 'root view',因此 include
不会覆盖其布局参数(我相信)。
当 View
或 ViewGroup
膨胀为不支持子指定的布局参数的 ViewGroup
时,这些参数将被丢弃。这在包含或膨胀(使用布局膨胀器)时发生。
如果子项 View
或 ViewGroup
添加到 ViewGroup
(使用 addView),则子项可能已经分配了布局参数,如果它们没有t 与正在添加的 ViewGroup
兼容,稍后在测量/布局期间可能会导致 class 转换异常。
你的第二个问题是关于 RelativeLayout
的布局规则,它的行为可以使用 android:layout_alignWithParentIfMissing
来定义
我想了解 <merge>
标签的内部工作原理。我已经使用 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"
- 在同一行中我们有另一个问题
@+id/some_text
不再存在,现在我们有@+id/bug
。 来看看结果
这里我有一些问题:
我们可以在合并文件中使用任何属性(用于任何布局),但是如果包含此部分的视图组(布局)(在我们的例子中 activity_main.xml)不有这样的属性,这种情况是如何解决的,正如我们在结果中看到的那样被忽略了?
我们在 imageview 上面硬编码了视图的 id,如果 id 存在,它就可以工作,但如果不存在,正如我们在结果中看到的那样,它也只是被忽略了
所以我已经写了很多了。 综上所述,XML 解析器的工作与合并和包含标记完全一致,了解 XML 解析器的源代码所在的位置,以浏览它也是很好的。
我将感谢所有阅读此行并提出建议或建议的人。
提前致谢。
如果 <include>
标签包含 layout_width 和 layout_height,它将覆盖所有根视图的(它包含的文件的)布局参数。
A merge
是一种避免在视图层次结构中增加深度的方法 - XML 布局文件必须只有一个根 - 所以它必须有一个 View
,一个 ViewGroup
,它可以包含额外的 View
或一个 <merge>
。当您在 ViewGroup
中包含具有多个 View
的布局时,您可能会向层次结构添加额外的不必要的复杂性 - 例如,可能不需要 2 个垂直方向的 LinearLayout
. merge
允许您删除多余的 ViewGroup
,并将其 View
合并到包含它的 ViewGroup
中。
包含带有 merge
和单个 View
的布局与直接包含带有单个 View
的布局相同,只是带有 merge
没有布局的 'root view',因此 include
不会覆盖其布局参数(我相信)。
当 View
或 ViewGroup
膨胀为不支持子指定的布局参数的 ViewGroup
时,这些参数将被丢弃。这在包含或膨胀(使用布局膨胀器)时发生。
如果子项 View
或 ViewGroup
添加到 ViewGroup
(使用 addView),则子项可能已经分配了布局参数,如果它们没有t 与正在添加的 ViewGroup
兼容,稍后在测量/布局期间可能会导致 class 转换异常。
你的第二个问题是关于 RelativeLayout
的布局规则,它的行为可以使用 android:layout_alignWithParentIfMissing