RecyclerView项目ConstraintLayout在使用GridLayoutManager时弄乱了

时间:2018-11-10 07:11:01

标签: android android-recyclerview android-constraintlayout gridlayoutmanager

我在 A 0 1 A 0 2 A 0 3 中使用 A 0 1 1 2 2 3 的{​​{1}}为2。 以下是GridLayoutManager项目的布局。它是一个RecyclerView,包裹在spanCount中:

RecyclerView

ConstraintLayout倾向于根据第二列中项目的高度增加CardView项目的高度时,就会出现问题。两个<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时,两个图标都位于底部。

谢谢

2 个答案:

答案 0 :(得分:2)

好吧,有了一些成功的尝试,然后让@Pawel Laskowski进一步回答,这是解决问题的方法:

  1. app:layout_constraintVertical_bias="1"属性添加到iv_locked
  2. android:layout_height中的ConstraintLayoutwrap_content更改为match_parent

ConstraintLayout很容易上手,但很难掌握。感谢@Pawel Laskowski的帮助。

答案 1 :(得分:1)

要使iv_locked与其垂直约束之间的底部对齐,您需要通过添加app:layout_constraintVertical_bias="1"属性来设置最大垂直偏差 对此。由于iv_favorite垂直约束在iv_locked上,因此它将自动处于适当的位置。