0

问题

我需要为我的应用程序使用 BottomSheetDialog (com.google.android.material.bottomsheet),但它没有按我预期的那样工作,底部工作表在展开时出现剪切。

在此处输入图像描述

我的实现

inline fun <T : ViewBinding> Context.makeBottomSheetDialog(
        crossinline bindingInflater: (LayoutInflater) -> T,
        isCancelable: Boolean = true,
        isHideable: Boolean = true,
        isFitContent: Boolean = true,
        peekHeight: Int? = null,
        onDismissListener: DialogInterface.OnDismissListener? = null,
): Pair<T, BottomSheetDialog> {
    val layout = bindingInflater.invoke(LayoutInflater.from(this@makeBottomSheetDialog))
    val dialog = BottomSheetDialog(this).apply {
        setContentView(layout.root)
        setOnDismissListener(onDismissListener)
        setCancelable(isCancelable)
    }.apply {
        behavior.apply {
            setHideable(isHideable)
            isFitToContents = isFitContent
            if(peekHeight != null) setPeekHeight(peekHeight)
        }
    }
    return Pair(layout, dialog)
}

我已经研究过这个问题,每个人都建议创建自己的类,但就我而言,我希望它具有灵活的视图并且易于使用内联调用。当我看到 BottomSheetDialog 的基本代码时,我认为这是因为展开 BottomSheet 时容器(FrameLayout)的高度没有调整。

问题

我该如何解决这个问题?它使我也无法在视图底部附加按钮。谢谢!

4

1 回答 1

0

有一种方法可以做到这一点:

在对话框的 XML 布局中:


 <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:id="@+id/sliderLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:behavior_hideable="true"
            app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

        // Place your layout code here and your code should be in one tag that can be any Layout.

        </RelativeLayout>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

在调用对话框的视图中,创建一个类型的BottomSheetBehavior变量RelativeLayout

private lateinit var bottomSheetBehaviour: BottomSheetBehavior<RelativeLayout>

然后,像这样调用你的对话框,


 val dialog = BottomSheetDialog(this)
 val dialogBinding = binding of your dialog layout
 dialog.setContentView(dialogBinding.root)
 bottomSheetBehaviour = BottomSheetBehavior.from(dialogBinding.sliderLayout)
 dialog.show()

 bottomSheetBehaviour.state = BottomSheetBehavior.STATE_EXPANDED
 bottomSheetBehaviour.peekHeight = dialogBinding.sliderLayout.height

当用户向上滑动布局时,这将使您的对话框布局全屏显示。

于 2021-01-28T05:08:55.173 回答