简体   繁体   English

多次包含相同的布局

[英]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). 我有一个共同的布局(common.xml),我希望在另一个布局(layout_a.xml)中包含很多次。 But it only shows me just one time. 但它只给我看了一次。 Why? 为什么?

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 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. 在XML中定义的ID必须是唯一的。 You are including two layouts that have views that contain the same id. 您包含两个具有包含相同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. ps除非您的第一个布局文件中没有包含更多代码,否则该合并标记将毫无用处。

As btse said, the ids in the XML must be unique. 正如btse所说,XML中的ID必须是唯一的。 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 . 这就是我在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. test1只是一个用LinearLayout(Vertical)制作的布局,带有文本和一个按钮和mainofferslayout在这种情况下,主要布局带有图像。

// 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来修复,因为它们是重叠的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 android-xml-多次包含相同布局不起作用 - android - xml - include same layout multiple times does not work 在布局中多次使用相同的视图 - Using same views multiple times in a layout 多次添加同一视图-重复使用布局 - Add same view multiple times - Reuse a layout 多次包含布局时在布局内设置Android RemoteViews的文本 - Set text of Android RemoteViews inside layout when include it multiple times 在相同的活动/布局中多次使用相同的片段 - Use same fragment multiple times in same activity/layout 在活动布局中多次合并/包含xml布局文件的最佳解决方案 - Best solution to merge/include an xml layout file multiple times in an activity layout 如何在运行时多次添加相同的布局; - How to add same layout multiple times during runtime; Android 重复使用布局时自动填充多次输入相同的信息 - Android autofill enters the same info multiple times, when reusing layout 如何处理动态膨胀的布局内容(多次膨胀相同的布局) - How to handle dynamically inflated layout content (Same Layout inflated multiple times) 如何在同一应用程序项目中多次将一个库模块包含到多个库模块中? - How to include a library module multiple times into multiple library modules in the same app project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM