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