Android 使用GridLayoutManager时,RecyclerView项约束显示出错

Android 使用GridLayoutManager时,RecyclerView项约束显示出错,android,android-recyclerview,android-constraintlayout,gridlayoutmanager,Android,Android Recyclerview,Android Constraintlayout,Gridlayoutmanager,我在我的RecyclerView中使用GridLayoutManager,其spanCount为2。 以下是RecyclerView项目的布局。它是一个ConstraintLayout,被包装在CardView中: <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.co

我在我的
RecyclerView
中使用
GridLayoutManager
,其
spanCount
为2。 以下是
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
增加时,两个图标都位于底部


感谢要使
iv_锁定
在其垂直约束之间与底部对齐,您需要通过添加
app:layout_constraintVertical_bias=“1”
属性来设置最大垂直偏移
去吧。由于
iv_favorite
垂直约束到
iv_locked
,它将自动处于适当的位置。

好的,通过一些点击和尝试,并将@Pawel Laskowski的答案再进一步,以下是解决问题的方法:

  • app:layout\u constraintVertical\u bias=“1”
    属性添加到
    iv\u locked
  • ConstraintLayout
    android:layout\u height
    wrap\u content
    更改为
    match\u parent

  • ConstraintLayout
    很容易入门,但很难掌握。谢谢@Pawel Laskowski的帮助。

    我已经尝试过使用垂直偏差,但不知何故也不起作用。这是正确的,很好的发现<代码>匹配父项
    的高度是
    ConstraintLayout
    填充
    CardView
    的高度所必需的。否则就没有什么区别了。很容易错过。