我试图使我的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
布局中,但仍然有相同的结果。
发布于 2022-03-21 14:37:37
使用这个
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
}
https://stackoverflow.com/questions/71559265
复制相似问题