Android 如何从片段中动态地将多个子布局视图添加到MaterialCardView?
在我的一个片段中,我有一个CardView,我想根据布局文件在其中添加多个子视图。添加的子视图的数量取决于列表对象中有多少项(因此我使用for循环来迭代列表)。当我向cardview添加视图时,应用程序会崩溃,并出现一个错误,提示我必须在添加其他视图之前删除子视图Android 如何从片段中动态地将多个子布局视图添加到MaterialCardView?,android,android-layout,android-fragments,kotlin,Android,Android Layout,Android Fragments,Kotlin,在我的一个片段中,我有一个CardView,我想根据布局文件在其中添加多个子视图。添加的子视图的数量取决于列表对象中有多少项(因此我使用for循环来迭代列表)。当我向cardview添加视图时,应用程序会崩溃,并出现一个错误,提示我必须在添加其他视图之前删除子视图 <?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android"
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import type="com.mahaprasad.mahaprasad.models.DeliveryMethod" />
<import type="android.view.View" />
<variable
name="viewModel"
type="com.mahaprasad.mahaprasad.screens.checkoutflow.CheckoutSharedViewModel" />
</data>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.core.widget.NestedScrollView
android:id="@+id/nested_scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="50dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<com.google.android.material.card.MaterialCardView
android:id="@+id/basket_products_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp">
<!-- Layout Child Views Should go here -->
</com.google.android.material.card.MaterialCardView>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
<com.google.android.material.button.MaterialButton
android:id="@+id/checkout_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|start"
android:insetBottom="0dp"
android:text="Place Order"
android:textAlignment="viewStart"
app:backgroundTint="@color/green_button_colour"
app:cornerRadius="0dp" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>
为什么不将布局设置为CardView的子视图,并以编程方式将列表元素添加到此布局,而不是将它们全部添加到CardView?为什么不将布局设置为CardView的子视图,并以编程方式将列表元素添加到此布局,而不是将它们全部添加到CardView?只需添加一个
LinearLayout
在materialcardwiew
中,并将子视图直接插入布局MaterialCardView
扩展了CardView
,CardView
设计用于在其内部保存单个子视图
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp">
<LinearLayout
android:id="@+id/basket_products_card"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- Layout Child Views Should go here -->
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
只需在
MaterialCardView
中添加一个线性布局
,然后直接将子视图插入布局MaterialCardView
扩展了CardView
,CardView
设计用于在其内部保存单个子视图
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp">
<LinearLayout
android:id="@+id/basket_products_card"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- Layout Child Views Should go here -->
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp">
<LinearLayout
android:id="@+id/basket_products_card"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- Layout Child Views Should go here -->
</LinearLayout>
</com.google.android.material.card.MaterialCardView>