Linearlayout怎么将button悬浮放在Recyclerview里面
时间: 2023-12-10 20:38:06 浏览: 171
要将Button悬浮放在RecyclerView里面,可以使用CoordinatorLayout和AppBarLayout来实现。
1. 在布局文件中,使用CoordinatorLayout作为根布局,再在其中添加RecyclerView和Button。
```
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<Button
android:id="@+id/floating_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Floating Button"
app:layout_anchor="@id/recycler_view"
app:layout_anchorGravity="bottom|end"/>
</android.support.design.widget.CoordinatorLayout>
```
2. 在AppBarLayout中添加Toolbar,并将其作为RecyclerView的父布局。
```
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"/>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<Button
android:id="@+id/floating_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Floating Button"
app:layout_anchor="@id/recycler_view"
app:layout_anchorGravity="bottom|end"/>
</android.support.design.widget.CoordinatorLayout>
```
这样做可以让RecyclerView在滚动时自动隐藏Toolbar和Button,当RecyclerView滚动到顶部时再次显示。同时,Button会悬浮在RecyclerView上方,不会被RecyclerView遮挡。
阅读全文
相关推荐









