首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将BottomSheetDialogFragment设置为fullScreen?

如何将BottomSheetDialogFragment设置为fullScreen?
EN

Stack Overflow用户
提问于 2022-03-21 14:29:53
回答 1查看 294关注 0票数 0

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

我尝试将peekHeight设置为:

代码语言:javascript
运行
复制
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布局中,但仍然有相同的结果。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-21 14:37:37

使用这个

代码语言:javascript
运行
复制
 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中

代码语言:javascript
运行
复制
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
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71559265

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档