android - 多次包含相同的布局

标签 android android-layout

我有一个通用布局 (common.xml),我想将它多次包含在另一个布局 (layout_a.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>

最佳答案

在 XML 中定义的 ID 必须是唯一的。您包括两个布局,它们的 View 包含相同的 ID。

Here是你将如何修复它。

附注除非有更多代码未包含在您的第一个布局文件中,否则该合并标记是无用的。

关于android - 多次包含相同的布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19195454/

相关文章:

java - 如何从另一个android xml文件中通过id获取颜色

来自其他 Activity 的 Android 调用方法

android - Flutter/Dart应用程序在构建小部件时引发异常

android - 我们如何为 Android N 证书固定生成 X.509 证书的 SubjectPublicKeyInfo 的 Base64 编码的 SHA256 哈希?

android - Android 上的定期订阅

android - XML 代码中的布局属性在使用 addView 方法时会被忽略

android - 在 Android 中使用 ASyncTask 加载布局

android - TabLayout 选项卡文本未显示

Android TabsAdapter 处理 fragment

java - 从 AttributeSet 获取颜色