展开时如何防止FAB覆盖BottomSheet?
How to prevent FAB overlaying BottomSheet when Expanded?
我在我的 Android 应用程序中使用 Material FAB 和 BottomSheet,我在覆盖 BottomSheet 的 FAB 上遇到了一些问题,我会阻止它。
实际上,当 BottomSheet1 完全展开时,我有两个 BottomSheet,我希望 FAB 位于 BottomSheet 后面,而当 BottomSheet2 处于半展开状态时,我希望它位于 BottomSheet 之上,并在它完全展开时使其位于 BottomSheet 后面。
我已经尝试使 BottomSheet 的高度高于 FAB 的高度,但无论如何都行不通。
我的代码如下所示:
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LetturaActivity">
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bottomAppBar"
style="@style/Widget.MaterialComponents.BottomAppBar.Colored"
android:layout_gravity="bottom"
app:navigationIcon="@drawable/ic_baseline_menu_24"
app:menu="@menu/bottom_app_bar"
app:hideOnScroll="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" \>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fabNuovo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_add"
android:elevation="4dp"
app:tint="@color/white"
android:contentDescription="@string/nuovo_documento"
app:layout_anchor="@id/bottomAppBar"
/>
<include layout="@layout/bottom_sheet_testata" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
BottomSheet parent 布局如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/bottomSheetTestata"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
app:behavior_hideable="true"
android:elevation="5dp"
android:clickable="false"
android:focusable="false"
app:behavior_peekHeight="0dp"
app:layout_behavior="@string/bottom_sheet_behavior"
android:orientation="vertical">
...
<\LinearLayout>
但是BottomSheet展开后是这样的:
不是重叠就是重叠,问题是高程。
从 documentation 您可以看到所有元素都有不同的高度。
Fab 是 6dp
,虽然 BottomSheet
不好找,应该是 16dp
(您可以使用浏览器功能搜索关键字 bottom
)
所以将您的海拔高度更改为
android:elevation="16dp"
我在我的 Android 应用程序中使用 Material FAB 和 BottomSheet,我在覆盖 BottomSheet 的 FAB 上遇到了一些问题,我会阻止它。
实际上,当 BottomSheet1 完全展开时,我有两个 BottomSheet,我希望 FAB 位于 BottomSheet 后面,而当 BottomSheet2 处于半展开状态时,我希望它位于 BottomSheet 之上,并在它完全展开时使其位于 BottomSheet 后面。
我已经尝试使 BottomSheet 的高度高于 FAB 的高度,但无论如何都行不通。
我的代码如下所示:
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LetturaActivity">
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bottomAppBar"
style="@style/Widget.MaterialComponents.BottomAppBar.Colored"
android:layout_gravity="bottom"
app:navigationIcon="@drawable/ic_baseline_menu_24"
app:menu="@menu/bottom_app_bar"
app:hideOnScroll="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" \>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fabNuovo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_add"
android:elevation="4dp"
app:tint="@color/white"
android:contentDescription="@string/nuovo_documento"
app:layout_anchor="@id/bottomAppBar"
/>
<include layout="@layout/bottom_sheet_testata" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
BottomSheet parent 布局如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/bottomSheetTestata"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
app:behavior_hideable="true"
android:elevation="5dp"
android:clickable="false"
android:focusable="false"
app:behavior_peekHeight="0dp"
app:layout_behavior="@string/bottom_sheet_behavior"
android:orientation="vertical">
...
<\LinearLayout>
但是BottomSheet展开后是这样的:
不是重叠就是重叠,问题是高程。
从 documentation 您可以看到所有元素都有不同的高度。
Fab 是 6dp
,虽然 BottomSheet
不好找,应该是 16dp
(您可以使用浏览器功能搜索关键字 bottom
)
所以将您的海拔高度更改为
android:elevation="16dp"