如何将BottomSheetDialogFragment设置为全屏



我正试图让我的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
}
}

但是Dialog显示为与没有peekHeight相同。

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

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

使用此

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
}

在对话框中调用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
}

最新更新