简体   繁体   中英

Include same layout multiple times

I have a common layout (common.xml) which I want to include many times in another layout (layout_a.xml). But it only shows me just one time. Why?

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>

The ids when defined in XML must be unique. You are including two layouts that have views that contain the same id.

Here is how you would go about fixing it.

ps Unless there is more code that you are not including in your first layout file, that merge tag is useless.

As btse said, the ids in the XML must be unique. It can be achieved in this way:

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

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

For information about how to access the elements inside those two included views, you can check out this blog post.

That's what I had done in my Project with Inflater . test1 is just a Layout made with a LinearLayout(Vertical) with text and a button and mainofferslayout in that case, the main layout with an image.

// Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_offers_display, container, false);

    View inflatedLayout = inflater.inflate(R.layout.test1, (ViewGroup) view, false);
    LinearLayout ll = (LinearLayout) view.findViewById(R.id.mainofferslayout);

    ll.addView(inflater.inflate(R.layout.test1, (ViewGroup) view, false));
    ll.addView(inflater.inflate(R.layout.test1, (ViewGroup) view, false));
    ll.addView(inflater.inflate(R.layout.test1, (ViewGroup) view, false));

我通过将RelativeLayout的layout_height设置为250dp来修复,因为它们是重叠的。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM