我在 RecyclerView
中使用 GridLayoutManager
,spanCount
为 2。
以下是 RecyclerView
项的布局。它是一个包裹在 CardView
中的 ConstraintLayout
:
<android.support.v7.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"
android:layout_margin="@dimen/cardview_margin"
android:foreground="?android:attr/selectableItemBackground"
app:cardBackgroundColor="@android:color/white"
app:cardCornerRadius="4dp">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="Title"
android:textAllCaps="true"
android:textColor="@color/colorPrimary"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv_sub_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Subtitle"
android:textAllCaps="false"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="@+id/tv_title"
app:layout_constraintStart_toStartOf="@+id/tv_title"
app:layout_constraintTop_toBottomOf="@+id/tv_title" />
<ImageView
android:id="@+id/iv_favorite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:scaleX="1.07"
android:scaleY="1.07"
android:src="@drawable/ic_star_blank_grey"
app:layout_constraintBottom_toBottomOf="@+id/iv_locked"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/iv_locked"/>
<ImageView
android:id="@+id/iv_locked"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:scaleX="1.0"
android:scaleY="1.0"
android:src="@drawable/ic_lock_open_grey"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/iv_favorite"
app:layout_constraintTop_toBottomOf="@+id/tv_sub_title" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
当 GridLayoutManager 倾向于根据第二列中项目的高度增加 RecyclerView 项目的高度时,就会出现问题。两个 ImageView
图标都离开父级底部,并倾向于粘在 TextView
tv_sub_title
上,如下面的屏幕截图所示:
请告诉我如何解决此问题,以便当 GridLayoutManager
增加项目布局的高度时,两个图标都保持在底部。
谢谢
最佳答案
好的,经过一些点击和尝试,并进一步考虑 @Pawel Laskowski 的回答,以下是解决问题的方法:
- 将
app:layout_constraintVertical_bias="1"
属性添加到iv_locked
- 将
ConstraintLayout
的android:layout_height
从wrap_content
更改为match_parent
。
ConstraintLayout
很容易上手,但很难掌握。感谢@Pawel Laskowski 的帮助。
关于android - 使用 GridLayoutManager 时 RecyclerView 项目 ConstraintLayout 困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53236799/