Android:从另一个xml引用xml

Jad*_*Jad 1 xml android android-layout android-xml

我正在尝试在android中设置标签布局.在id为'tab1'的布局中,有什么办法可以引用另一个xml文件吗?我只是不想有一个巨大的凌乱单个文件.相反,我想在每个布局中使用ids'tab1','tab2'和'tab3'引用不同的文件.

这是标签布局:

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

<TabHost
    android:id="@+id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab2"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab3"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>
</TabHost>


</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

And*_*ler 7

您可以使用includeandroid的标记添加其他xml文件,如下所示示例:

假设下面的布局是在xml中命名为layout1.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical" >
    <TextView android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Hello, I am a TextView" />
    <Button android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello, I am a Button" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

然后我们可以在另一个布局xml中添加上面的xml,它也可能有其他小部件,如下所示:

<include layout="@layout/layout1" />
Run Code Online (Sandbox Code Playgroud)

是对android developpement网站的引用