java - 带 FAB 的模态 Bottom Sheet

标签 java android android-layout floating-action-button bottom-sheet

我已经实现了持久性 Bottom Sheet ,FAB 固定在它的顶部。

bottom sheet

当我尝试对模态 Bottom Sheet (扩展 BottomSheetDialogFragment)做同样的事情时,它说

java.lang.IllegalStateException: Could not find CoordinatorLayout descendant view to anchor view android.support.design.widget.FloatingActionButton

是否可以使用模态 Bottom Sheet 进行相同的布局,或者在持久性 Bottom Sheet 上方制作阴影和不可点击区域?

最佳答案

我试图在 ModalBottomSheet 中做同样的“FAB on top”事情,但是没有直接的方法/方式来做到这一点。
对于这种情况,我们因此使用 FrameLayout .在定义里面,它说 -

Child views are drawn in a stack, with the most recently added child on top.

因此我们可以为 ModalBottomSheet(扩展 BottomSheetDialogFragment)创建一个布局,在您的情况下它看起来像这样 -

<FrameLayout
    android:id="@+id/parent_frame_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context=".ModalBottomSheetFragment">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="25dp"
        android:orientation="vertical">

        <!-- Whatever layout you want to give here. This is going to be the body
        of the ModalBottomSheet. Also you might not wanna use the current Linear 
        Layout too.-->

    </LinearLayout>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/icon_sheet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="0dp"
        android:layout_gravity="center|top"
        android:src="@drawable/ic_music"
        android:elevation="20dp"/>

</FrameLayout>

注意 -

  1. FAB 是在 LinearLayout(BottomSheet 的主体)之后创建的,因为它将堆叠在 LinearLayout 之上。
  2. 我们为 LinearLayout 设置了 marginTop=25dp,但为 FAB 设置了 marginTop=0dp。这就是您如何创建半出半入的效果。并且还提高了 FAB 的高度,以获得更好的 float 外观。

最后,我们可以在 onCreateDialog 重写方法中扩展它的 View ,然后执行所有必要的操作。

希望我能够回答您的问题。对任何进一步的疑问/更新发表评论。

关于java - 带 FAB 的模态 Bottom Sheet ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42813603/

相关文章:

Android:使用具有相同中心的不同弧尺寸的drawArc()

Android - 在运行时更改 fragment 布局

android - 我的 apk 文件在模拟器中的位置

android - 从 Google Developer Console 导出所有堆栈跟踪

android - 窗口背景 : Image with selected color

javascript - Spring Boot 谷歌地图 SAXParseException

java - 无法找到主类。程序将会退出

java - 如何查找 Web 应用程序中加载页面的 cpu、io、内存利用率

Java程序拦截并加密流量?

android - 为什么 React Native WebView 会转到 :blank in release build? 左右