【发布时间】:2021-09-26 13:07:31
【问题描述】:
我正在尝试将 RecyclerView 放入 CardView 并动态创建项目。
显然,在XML 中,RecyclerView 设置在CardView tag 内部,但如您所见,RecyclerView 项目是在CardView 外部创建的。
(RecyclerView's items是照片中kg和set表示的部分。而CardView也是另一个RecyclerView的一个项目。)
有什么问题?
XML
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="12dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
app:cardElevation="10dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/ll1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/workout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="TEST"/>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="@color/light_blue_400"/>
</LinearLayout>
<LinearLayout
android:id="@+id/ll2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintTop_toBottomOf="@+id/ll1">
<com.google.android.material.button.MaterialButton
android:id="@+id/delete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginStart="8dp"
android:layout_marginEnd="4dp"
style="@style/RoutineButtonStyle"
android:text="DELETE"
app:icon="@drawable/ic_delete"/>
<com.google.android.material.button.MaterialButton
android:id="@+id/add"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginStart="4dp"
android:layout_marginEnd="8dp"
style="@style/RoutineButtonStyle"
android:text="ADD"
app:icon="@drawable/ic_add"/>
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/ll2"
android:orientation="vertical"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
如果您需要任何其他代码,请告诉我。
【问题讨论】:
-
你昨天问了这个问题。又不行了。可能会改进您对错误的解释。 “Set”和“kg”是否应该在 cardView 内?描述哪些元素是错误的。在您的图像上带有箭头。只有主要元素和一段代码,没有人可以帮助你。如果增加,我相信我可以为您提供解决方案
-
对不起。昨天的帖子被删除了,因为根本没有任何回应。并且文章略有编辑。
-
如果我在家,我会尽快为您提供帮助。知道如何解决这个问题。
-
使用 LayoutInspector 检查视图的边界。此外,您嵌套的 LinearLayouts 是一种浪费 - 您可以直接在 ConstraintLayout 中对齐“TEST”、Divider 和按钮 - 这就是它的重点。
-
我的回答解决了你的问题吗?
标签: android android-recyclerview android-cardview