同一视图不能多次动态添加

时间:2019-09-16 09:40:23

标签: android android-linearlayout android-inflate

我正在尝试向片段中动态添加几个cardview,它们的基础是从xml文件中获取的同一cardview,但是无论我尝试添加多少,它只会添加一个:

这是我用来尝试添加它们的代码:

linearLayout=binding.initial


       val inflater = LayoutInflater.from(context);

        val  view = inflater.inflate(R.layout.layout_horas_extra, null)

        val  view2 = inflater.inflate(R.layout.layout_horas_extra, null)

        val  view3 = inflater.inflate(R.layout.layout_horas_extra, null)

       linearLayout.addView(view)

        linearLayout.addView(view2)

        linearLayout.addView(view3)

这是我从中加载布局的初始部分:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
        android:id="@+id/requestViewCardTimePicker"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:padding="2dp"
        xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="1dp">

        <TextView
                android:id="@+id/diaSeleccionado"
                style="@style/Widget.AppCompat.Button.Borderless"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="4dp"
                android:layout_weight=".50"
                android:text="01/09/2019"
                android:textAlignment="textStart"
                />

        <Spinner
                android:id="@+id/planets_spinner"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="12dp"
                android:layout_marginTop="4dp"
                android:layout_marginEnd="6dp"
                android:layout_weight=".5"
                android:gravity="center"
                android:textAlignment="center"
                android:textSize="12dp" />

    </LinearLayout>

这是我尝试在以下位置创建卡片视图的线性布局:

  <androidx.appcompat.widget.LinearLayoutCompat
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:id="@+id/initial">

我在做什么错了?

1 个答案:

答案 0 :(得分:1)

已经问过类似的问题。您可以在此处查看答案。 https://stackoverflow.com/a/9106698/12019759

相关问题