0

これは奇妙な質問に思えるかもしれませんが、メインの XML ファイルが指す他の xml ファイルのセットからレイアウト XML を作成できれば、非常に便利です。その理由は、この xml で定義されたいくつかのリスト アイテム ビューがあり、他の場所で再利用したいからです。それは可能ですか、またはそれを行う唯一の方法は、それをコピーして貼り付けるだけですか?

4

3 に答える 3

3

「include」タグを使用して、1 つのレイアウトに異なるレイアウト ファイルを含めることができます。

<LinearLayout>
    <include layout="@layout/toinclude1" />
    <include layout="@layout/toinclude1" />
</LinearLayout>

もう 1 つの方法は、ViewStub です。レイアウトを非同期的にロードする場合は、次のようにします。

<ViewStub android:id="@+id/stub"
           android:inflatedId="@+id/subTree"
           android:layout="@layout/mySubTree"
           android:layout_width="120dip"
           android:layout_height="40dip" />

そして、必要に応じてコード内で次のように記述できます。

 ViewStub stub = (ViewStub) findViewById(R.id.stub);
 View inflated = stub.inflate();

参照用: http://developer.android.com/reference/android/view/ViewStub.html

于 2012-06-27T15:47:22.567 に答える
1

このような header.xml があるとします

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/somestyle" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:clickable="false"
        android:paddingLeft="15dip"
        android:scaleType="center"
        android:src="@drawable/logo" />

</LinearLayout>

を使用<include layout="@layout/header"/>して、多くのレイアウトにヘッダー レイアウト コードを含めることができます。main.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/home_root"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
            <include layout="@layout/header"/>
</LinearLayout>
于 2012-06-27T15:48:10.757 に答える
0

フラグメントは良いオプションです。例を次に示します: http://mobile.tutsplus.com/tutorials/android/android-sdk_fragments/
およびドキュメント: http://developer.android.com/guide/components/fragments.html

于 2012-06-27T15:51:20.103 に答える