Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 为什么按钮在RecyclerView上浮动?_Android_Android Recyclerview_Android Constraintlayout - Fatal编程技术网

Android 为什么按钮在RecyclerView上浮动?

Android 为什么按钮在RecyclerView上浮动?,android,android-recyclerview,android-constraintlayout,Android,Android Recyclerview,Android Constraintlayout,我有这样的布局: <?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" xmln

我有这样的布局:

<?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
布局高度设置为
match\u parent
,它将占据应用程序的整个高度。要解决此问题,您需要将其约束设置为注销按钮,并将布局高度设置为
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>

不要在约束列表中使用
匹配父项<代码>匹配\u父项
在逻辑上与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"
   />

循环视图与父对象匹配,甚至与父对象的底部对齐

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

默认按钮样式包括
elevation
,这会导致它们被绘制在顶部,即使它们位于视图层次结构中的
RecyclerView
下。@tynn-Ah抱歉。修正了。刚刚更新了代码。
<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" />