包含标签
该标签可让您将布局划分为多个文件:它可以帮助处理复杂的或超长用户界面。
假设您使用两个包含文件来分割复杂的布局,如下所示:
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已经睡得更好了。
你现在不快乐吗?