Почему кнопки плавают поверх RecyclerView?

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

<?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>

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

Почему кнопки плавают поверх RecyclerView?

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

Я хочу, чтобы кнопки были зафиксированы под RecyclerView. Как это исправить?

Стиль кнопок по умолчанию включает elevation, из-за чего они отображаются сверху, даже если они находятся под RecyclerView в иерархии представлений.

Pawel 10.02.2019 15:05

@tynn А, извини. Исправлено. Просто обновил код.

anta40 11.02.2019 05:27
2
2
468
4
Перейти к ответу Данный вопрос помечен как решенный

Ответы 4

Ответ принят как подходящий

вы установили layout_heightrecyclerView на 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>

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

    <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>

Не используйте 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"
   />

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" />

Другие вопросы по теме