【问题标题】:FloatingActionButton inside constraintLayout in ScrollViewScrollView 中的约束布局内的 FloatingActionButton
【发布时间】:2021-06-28 12:06:44
【问题描述】:
我在 ScrollView 内的 ConstraintLayout 中有一个 floatingActionButton
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_grey">
<androidx.constraintlayout.widget.ConstraintLayout>
----
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/bag_background"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
/>
-----
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
问题是按钮随着 UI 上下滚动
Ps:不知道重要不重要,但是scrollView在drawerLayout里面
【问题讨论】:
标签:
android
xml
button
android-constraintlayout
floating-action-button
【解决方案1】:
您可以将您的根视图设置为 FrameLayout 然后在其中,您将使用 ScrollView 然后是 FAB 按钮,如下所示
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_grey">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_grey">
<androidx.constraintlayout.widget.ConstraintLayout>
<!-- your view xml here -->
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:src="@drawable/bag_background"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
/>
</FrameLayout>
【解决方案2】:
要放置浮动操作按钮,请使用CoordinatorLayout。 CoordinatorLayout 有助于促进其中包含的视图之间的交互,这将有助于描述如何根据滚动变化为按钮设置动画。
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|bottom"
android:layout_margin="16dp" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>