ConstraintLayout - 下面的RecyclerView按钮不遵守约束

Sri*_*ith 4 android android-constraintlayout

我在ConstraintLayout中有一个RecyclerView和一个按钮.我希望Button位于顶部,而RecyclerView位于其下方.尝试设置app:layout_constraintTop_toBottomOf="@+id/button"为RecyclerView,但它不尊重.它看起来像这样:

结果视图

我想在Button下方使用RecyclerView.这是我的代码:

<android.support.constraint.ConstraintLayout
    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="match_parent">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="48dp"
        android:layout_marginTop="16dp"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button" />

</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

parent为Button 设置了开始,顶部,结束和底部约束.对于RecyclerView,开始和结束parent以及顶部约束到底部button

我究竟做错了什么?

Moh*_*ara 8

我认为这应该解决它:

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="48dp"
    android:layout_marginTop="16dp"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0" />

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerview"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="8dp"
    android:layout_marginTop="16dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/button" />
Run Code Online (Sandbox Code Playgroud)

如果您使用布局遇到问题使用相对或线性并转换它.

编辑:

说明:

  • 添加

应用程式:layout_constraintBottom_toBottomOf = "父"

将recyclerview约束到父级的底部.

  • 将宽度和高度归零以消除任何重写冲突.


Eug*_*sov 5

使用ConstraintLayout构建响应式UI

您不能将Cons_parent用于ConstraintLayout中的任何视图。而是使用“匹配约束”(0dp)。