Почему кнопки плавают поверх RecyclerView? - PullRequest
0 голосов
/ 10 февраля 2019

У меня есть такой макет:

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

    <android.support.v7.widget.RecyclerView
        app:layout_constraintBottom_toBottomOf="parent"
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <Button
        android:id="@+id/btn_buy_more"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="Buy More"
        app:layout_constraintBottom_toBottomOf="@id/recycler_view" />

    <Button
        android:id="@+id/btn_checkout"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="Checkout"
        app:layout_constraintBottom_toTopOf="@id/btn_buy_more" />

    <Button
        android:id="@+id/btn_logout"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="Logout"
        app:layout_constraintBottom_toTopOf="@id/btn_checkout" />

</android.support.constraint.ConstraintLayout>

Это результат:

enter image description here

RecyclerView заполняет всю высоту экрана,И кнопки расположены не под RecyclerView, а над ним.Это не то, что я хочу.

То, что я хочу, это кнопки, расположенные неподвижно под RecyclerView.Как это исправить?

Ответы [ 4 ]

0 голосов
/ 11 февраля 2019

RecyclerView соответствует родительскому элементу и даже выравнивается по низу родительского элемента.

<android.support.v7.widget.RecyclerView
    app:layout_constraintBottom_toBottomOf="parent"
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Вместо этого следует ограничить его кнопками и сопоставить ограничения (высота или ширина 0dp).

<android.support.v7.widget.RecyclerView
    app:layout_constraintBottom_toTopOf="@id/btn_logout"
    app:layout_constraintTop_toTopOf="parent"
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="0dp" />
0 голосов
/ 10 февраля 2019

вам нужно внести некоторые изменения, чтобы правильно установить кнопки

    <android.support.v7.widget.RecyclerView
       app:layout_constraintBottom_toBottomOf="parent"
       android:id="@+id/recycler_view"
       android:layout_width="match_parent"
       android:layout_height="match_parent" />
    </android.support.v7.widget.RecyclerView>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="warp_content"
        android:orientation="horizontal"/>

    <Button
     android:id="@+id/btn_buy_more"
     android:layout_width="match_parent"
     android:layout_height="50dp"
     android:text="Buy More"
     />

     <Button
        android:id="@+id/btn_checkout"
        android:layout_width="match_parent"
         android:layout_height="50dp"
        android:text="Checkout"
       />

    <Button
    android:id="@+id/btn_logout"
    android:layout_width="match_parent"
    android:layout_height="50dp"
     android:text="Logout"
     />

  </RelativeLayout>
0 голосов
/ 10 февраля 2019

Не используйте match_parent внутри ConstraintLayout .match_parent логически не связан с ConstraintLayout .Добавьте ваши ограничения следующим образом:

<android.support.v7.widget.RecyclerView
    app:layout_constraintBottom_toBottomOf="parent"
    android:id="@+id/recycler_view"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toTopOf="@+id/btn_logout"
   />
0 голосов
/ 10 февраля 2019

Вы установили layout_height для recyclerView на match_parent, что займет всю высоту приложения.чтобы это исправить, вам нужно установить ограничение на кнопку выхода из системы и 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        app:layout_constraintBottom_toTopOf="@id/btn_logout"
        app:layout_constraintTop_toTopOf="parent"
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"/>

    <Button
        android:id="@+id/btn_buy_more"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="Buy More"
        app:layout_constraintBottom_toBottomOf="parent" />

    <Button
        android:id="@+id/btn_checkout"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="Checkout"
        app:layout_constraintBottom_toTopOf="@id/btn_buy_more" />

    <Button
        android:id="@+id/btn_logout"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="Logout"
        app:layout_constraintBottom_toTopOf="@id/btn_checkout" />

</android.support.constraint.ConstraintLayout>
...