Как заставить RecyclerView заполнять свободное пространство, когда я использую ConstraintLayout? - PullRequest
0 голосов
/ 25 ноября 2018

Я новичок в ConstraintLayout из Android Studio 3.2.1, похоже, что ConstraintLayout сложно работать.

Я надеюсь, что четыре кнопки расположены внизу экрана, а элемент управления RecyclerView заполнит всесвободного места на экране, но следующий код не делает это, как я ожидал, вы можете увидеть следующее изображение.

Как я могу это исправить?

Кстати, измененный код можеттоже не работает.

Модифицированный код

  <android.support.v7.widget.RecyclerView
            android:id="@+id/mRecyclerView"
            android:layout_width="0dp"
            android:layout_height="0dp"

Код компоновки

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                             xmlns:app="http://schemas.android.com/apk/res-auto"
                                             xmlns:tools="http://schemas.android.com/tools"
                                             android:layout_width="match_parent"
                                             android:layout_height="match_parent">


    <ImageButton
            android:id="@+id/btnOne"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"                
            app:srcCompat="@drawable/delete32"

            app:layout_constraintHorizontal_chainStyle="spread"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@+id/btnAddEdit" />

    <ImageButton
            android:id="@+id/btnAddEdit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"               
            app:srcCompat="@drawable/delete32"

            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toRightOf="@+id/btnOne"
            app:layout_constraintRight_toLeftOf="@+id/btnThree" />


    <ImageButton
            android:id="@+id/btnThree"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"             
            app:srcCompat="@drawable/delete32"

            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toRightOf="@+id/btnAddEdit"
            app:layout_constraintRight_toLeftOf="@+id/btnFour" />

    <ImageButton
            android:id="@+id/btnFour"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"               
            app:srcCompat="@drawable/delete32"

            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toRightOf="@+id/btnThree"
            app:layout_constraintRight_toRightOf="parent" />


    <android.support.v7.widget.RecyclerView
            android:id="@+id/mRecyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toBottomOf="parent"
            app:layout_constraintBottom_toTopOf="@+id/btnOne"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"/>

</android.support.constraint.ConstraintLayout>

Изображение

enter image description here

Ответы [ 2 ]

0 голосов
/ 25 ноября 2018

Вы должны выровнять RecyclerView по верху родительского макета и установить для «android: layout_height» recyclerview значение 0dp.Я изменил код макета, и он работает нормально.Скопируйте его и сравните с этим.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
    android:id="@+id/btnOne"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_chainStyle="spread"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="@+id/btnAddEdit"
    app:srcCompat="@drawable/delete32" />

<ImageButton
    android:id="@+id/btnAddEdit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toRightOf="@+id/btnOne"
    app:layout_constraintRight_toLeftOf="@+id/btnThree"
    app:srcCompat="@drawable/delete32" />

<ImageButton
    android:id="@+id/btnThree"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toRightOf="@+id/btnAddEdit"
    app:layout_constraintRight_toLeftOf="@+id/btnFour"
    app:srcCompat="@drawable/delete32" />

<ImageButton
    android:id="@+id/btnFour"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toRightOf="@+id/btnThree"
    app:layout_constraintRight_toRightOf="parent"
    app:srcCompat="@drawable/delete32" />

<android.support.v7.widget.RecyclerView
    android:id="@+id/mRecyclerView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintBottom_toTopOf="@+id/btnAddEdit"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
0 голосов
/ 25 ноября 2018

Вы можете попробовать это:

<android.support.v7.widget.RecyclerView
        android:id="@+id/mRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/btnOne"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...