如何将 BottomSheetDialogFragment 设置为全屏?

Igo*_*yuk 1 android kotlin android-bottomsheetdialog

我试图让我的BottomSheetDialogFragment打开时全屏显示,问题是在任何情况下都会Dialog显示屏幕高度的一半。

我尝试将 peekHeight 设置为以下内容:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    dialog?.setOnShowListener { dialog ->
        val bottomSheetBehavior: BottomSheetBehavior<*> = (dialog as BottomSheetDialog).behavior
        bottomSheetBehavior.peekHeight = Resources.getSystem().displayMetrics.heightPixels
        bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED
    }
}
Run Code Online (Sandbox Code Playgroud)

Dialog显示的内容与没有 peekHeight 的情况相同。

然后我尝试添加android:theme="@android:style/Theme.Material.Light.NoActionBar.Fullscreen"

在我的BottomSheet布局中但仍然有相同的结果。

Ami*_*dey 6

用这个

 fun setupRatio(context: Context, bottomSheetDialog: BottomSheetDialog, percetage: Int) {
    //id = com.google.android.material.R.id.design_bottom_sheet for Material Components
    //id = android.support.design.R.id.design_bottom_sheet for support librares
    val bottomSheet =
        bottomSheetDialog.findViewById<View>(R.id.design_bottom_sheet) as FrameLayout
    val behavior: BottomSheetBehavior<*> = BottomSheetBehavior.from(bottomSheet)
    val layoutParams = bottomSheet.layoutParams
    layoutParams.height = getBottomSheetDialogDefaultHeight(context, percetage)
    bottomSheet.layoutParams = layoutParams
    behavior.state = BottomSheetBehavior.STATE_EXPANDED
}
Run Code Online (Sandbox Code Playgroud)

在对话框中将其调用到 onStart 中

override fun onStart() {
    super.onStart()
    setupRatio(requireContext(),dialog as BottomSheetDialog,100)
}

 private fun getBottomSheetDialogDefaultHeight(context: Context, percetage: Int): Int {
    return getWindowHeight(context) * percetage / 100
}

   private fun getWindowHeight(context: Context): Int {
    // Calculate window height for fullscreen use
    val displayMetrics = DisplayMetrics()
    (context as Activity?)!!.windowManager.defaultDisplay.getMetrics(displayMetrics)
    return displayMetrics.heightPixels
}
Run Code Online (Sandbox Code Playgroud)