多次包含相同的布局 [英] Include same layout multiple times

查看:111
本文介绍了多次包含相同的布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通用布局(common.xml),我想在另一个布局(layout_a.xml)中多次包含它.但这只显示了一次.为什么?

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>

推荐答案

在XML中定义的ID必须唯一.您将包括两个布局,这些布局的视图包含相同的ID.

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.

p.s.除非您没有在第一个布局文件中包含更多代码,否则该合并标记将毫无用处.

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

这篇关于多次包含相同的布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆