包含标签
<include> 标签可让您将布局划分为多个文件:它有助于处理复杂 或过长的用户界面。
假设您使用两个包含文件拆分复杂的布局,如下所示:
top_level_activity.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- First include file -->
<include layout="@layout/include1.xml" />
<!-- Second include file -->
<include layout="@layout/include2.xml" />
</LinearLayout>
那你需要写include1.xml和include2.xml。
请记住,包含文件中的 xml 在渲染时只是转储到您的 top_level_activity 布局中(很像 C 的 #INCLUDE 宏)。
包含文件是简单的简布局 xml。
include1.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textView1"
android:text="First include"
android:textAppearance="?android:attr/textAppearanceMedium"/>
...和include2.xml:
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/button1"
android:text="Button" />
看到了吗?没有什么花哨。
请注意,您仍然必须使用 xmlns:android="http://schemas.android.com/apk/res/android 声明 android 命名空间。
所以 top_level_activity.xml 的渲染版本是:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- First include file -->
<TextView
android:id="@+id/textView1"
android:text="First include"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<!-- Second include file -->
<Button
android:id="@+id/button1"
android:text="Button" />
</LinearLayout>
在您的 java 代码中,所有这些都是透明的:您的活动类中的 findViewById(R.id.textView1) 返回正确的小部件(即使该小部件是在与活动布局不同的 xml 文件中声明的)。
最重要的是:可视化编辑器可以流畅地处理事情。顶级布局以 包含的 xml 呈现。
情节变厚
由于包含文件是一个经典的布局xml文件,这意味着它必须有一个顶级元素。
因此,如果您的文件需要包含多个小部件,则必须使用布局。
假设include1.xml 现在有两个TextView:必须声明一个布局。让我们选择一个LinearLayout。
include1.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:text="Second include"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<TextView
android:id="@+id/textView2"
android:text="More text"
android:textAppearance="?android:attr/textAppearanceMedium"/>
</LinearLayout>
top_level_activity.xml 将呈现为:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- First include file -->
<LinearLayout
android:id="@+id/layout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:text="Second include"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<TextView
android:id="@+id/textView2"
android:text="More text"
android:textAppearance="?android:attr/textAppearanceMedium"/>
</LinearLayout>
<!-- Second include file -->
<Button
android:id="@+id/button1"
android:text="Button" />
</LinearLayout>
但是等一下LinearLayout这两个层次是多余的!
事实上,两个嵌套的LinearLayout 没有任何作用,因为两个TextView 可以包含在layout1 下完全相同的渲染。
那么我们能做些什么呢?
输入合并标签
<merge> 标签只是一个虚拟标签,它提供了一个顶级元素来处理这种冗余问题。
现在 include1.xml 变为:
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/textView1"
android:text="Second include"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<TextView
android:id="@+id/textView2"
android:text="More text"
android:textAppearance="?android:attr/textAppearanceMedium"/>
</merge>
现在 top_level_activity.xml 呈现为:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- First include file -->
<TextView
android:id="@+id/textView1"
android:text="Second include"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<TextView
android:id="@+id/textView2"
android:text="More text"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<!-- Second include file -->
<Button
android:id="@+id/button1"
android:text="Button" />
</LinearLayout>
你保存了一个层次结构,避免一个无用的视图:Romain Guy 已经睡得更好了。
你现在不开心吗?