16

別のレイアウト (layout_a.xml) に何度も含めたい共通のレイアウト (common.xml) があります。しかし、それは一度だけ私を示しています。なんで?

common.xml

<merge xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@android:drawable/alert_light_frame">

        <ImageView

            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:id="@+id/imageView"

            android:src="@drawable/test"

            android:scaleType="centerCrop" />

        <TextView

            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout_below="@+id/imageView"
            android:id="@+id/textView"
            android:text="test"
            android:layout_marginTop="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginLeft="10dp" />
    </RelativeLayout>
</merge>

layout_a.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

    <include layout="@layout/common" />
</LinearLayout>
4

4 に答える 4

13

XML で定義されている場合、ID は一意である必要があります。同じ ID を含むビューを持つ 2 つのレイアウトを含めています。

これを修正する方法は次のとおりです

ps 最初のレイアウト ファイルに含めないコードが他にない限り、そのマージ タグは役に立ちません。

于 2013-10-05T08:11:50.673 に答える
10

btseが言ったように、XML 内の ID は一意でなければなりません。これは次の方法で実現できます。

<include android:id="@+id/common1"
    layout="@layout/common" />

<include android:id="@+id/common2"
    layout="@layout/common" />

これら 2 つのビュー内の要素にアクセスする方法については、このブログ投稿をご覧ください。

于 2013-10-05T08:23:31.893 に答える
-3

RelativeLayout の layout_height が重なっているので 250dp に設定して修正しました。

于 2013-10-05T09:06:19.230 に答える