1

Androidでタブレイアウトを設定しようとしています。ID「tab1」のレイアウト内で、別の xml ファイルを参照する方法はありますか? 私は巨大で厄介な単一のファイルを持ちたくありません。代わりに、ID「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>
4

2 に答える 2

7

include以下に示す例のように、android のタグを使用して他の xml ファイルを追加できます。

以下のレイアウトが、layout1.xml という名前の 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>

次に、以下に示すように、上記の xml を別のレイアウト xml に追加できます。このレイアウト xml には、他のウィジェットも含まれる場合があります。

<include layout="@layout/layout1" />

Android開発サイトへの参照はこちら

于 2013-01-21T14:19:27.037 に答える
2

< include /> タグを見てください。 Android インクルードタグ

于 2013-01-21T14:16:25.217 に答える