Как установить 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, но все равно был тот же результат.

0
0
40
1
Перейти к ответу Данный вопрос помечен как решенный

Ответы 1

Ответ принят как подходящий

Использовать это

 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
}

что такое getBottomSheetDialogDefaultHeight?

NiceToMytyuk 21.03.2022 15:40

Я обновляю код, пожалуйста, проверьте его

Amit pandey 22.03.2022 05:53

Другие вопросы по теме