如何将 BottomSheetDialogFragment 设置为全屏?
How to set BottomSheetDialogFragment to fullScreen?
我试图让我的 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
}
我试图让我的 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
}