我正在尝试在CoordinatorLayout中使用RecyclerView。我有以下内容:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/red"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:titleEnabled="true"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleGravity="top"
app:expandedTitleMarginStart="64dp"
app:expandedTitleMarginTop="16dp"
>
<View
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="@color/black"/>
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
/>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/green"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="Press Button"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
问题是,当我将回收站视图滚动到最后一个项目时,直到该按钮还有很大的空隙,我才能看到Coordinator
布局的红色背景
好像是recyclerview结束,并且因为按钮的底部位于重力的下方,所以两者之间存在间隙。
我搞砸了吗?屏幕截图中的绿色部分是recyclerview的末尾,红色是CoordinatorLayout的背景。我想消除差距。
我不认为这里的按钮有问题。首先,尝试将CoordinatorLayout
的高度设置为match_parent
。注意任何更改。之后,如果需要,对RecyclerView
执行相同的操作。