1

アプリにはスクロール バナーを含むレイアウトがあり (XML を見るとまだ完成していません)、このバナーは他のアクティビティで使用されます。カスタムレイアウトにしたいので、X回コピーペーストしません。

これが私のXMLです(まあ...すべてが正しいかどうかはわかりませんので、この部分の批判は大歓迎です)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="match_parent" 
    android:id="@+id/baseID">

    <RelativeLayout
        android:layout_centerInParent="true"
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:id="@+id/id1"
        android:background="#ff00ff">
        <Button 
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginLeft="25dp"
            android:layout_marginRight="25dp"/>
    </RelativeLayout>

    <LinearLayout 
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:layout_alignTop="@id/id1"
            android:layout_alignBottom="@id/id1"
            android:id="@+id/id2"
            android:background="#08000000"  
            android:orientation="horizontal">

            <Button
                android:id="@+id/button1"
                android:layout_width="20dp"
                android:layout_height="fill_parent"
                android:layout_weight="1"

                android:onClick="onClick"
                android:text="1" />

            <TextView 
                android:layout_height="fill_parent"
                android:layout_width="wrap_content"
                android:text="to jest moj tekst"
                android:layout_weight="16"/>

            <Button
                android:id="@+id/button2"
                android:layout_width="20dp"
                android:layout_height="fill_parent"
                android:layout_weight="1"

                android:onClick="onClick"
                android:text="1" />

    </LinearLayout>

</RelativeLayout>

今のところ、このレイアウトにはバナーのみが含まれていますが、さらに多くのものがあります。

問題は次のとおりです。外部クラスに配置するにはどうすればよいですか?

RelativeLayout(int この場合) を拡張する新しいクラスを作成する必要があることはわかっています。しかし、その後は?このクラスにレイアウトを設定するにはどうすればよいですか?

また、いくつかの調査を行いましたが、これに関する簡単で正確なチュートリアルは見つかりませんでした。知っている場合は、投稿してください。

4

2 に答える 2

1

次のように <include> を使用できます。

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

次のように、含まれている xml レイアウトのルート タグの属性を書き換えることもできます。

  <include android:id="@+id/my_menu" layout="@layout/menu" />

詳細な説明については、開発者ブログを参照してください。

http://android-developers.blogspot.com.es/2009/02/android-layout-tricks-2-reusing-layouts.html

于 2012-10-22T23:21:37.527 に答える
0

Fragment を使用する必要があります: http://developer.android.com/reference/android/app/Fragment.html

フラグメントを使用すると、開発者は VIEW/Controller をさまざまなクラスに分割できます。

したがって、xml に異なるフラグメントを追加し、各フラグメントが独自のコンポーネント (テキストビュー、ボタンなど) を担当します。

于 2012-10-22T15:06:48.600 に答える