使用BottomSheetDialogFragment移除底部菜单中的暗色背景

10
我正在使用BottomSheetDialogFragment展示底部弹窗。如何去掉昏暗的背景?
我已经将背景设置为透明,但当底部弹窗出现时,在它下面的背景会变暗。
class ClearDataBottomSheet : BottomSheetDialogFragment {

private lateinit var contentView: View

constructor() {

}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
}

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    val dialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog

    dialog.setCancelable(false)
    dialog.setOnShowListener { dialog ->
        val d = dialog as BottomSheetDialog
        val bottomSheet = d.findViewById<FrameLayout>(com.google.android.material.R.id.design_bottom_sheet)
        BottomSheetBehavior.from(bottomSheet!!).state = BottomSheetBehavior.STATE_EXPANDED
    }

    // Do something with your dialog like setContentView() or whatever
    return dialog
}

override fun setupDialog(dialog: Dialog, style: Int) {
    super.setupDialog(dialog, style)
    contentView = View.inflate(context, R.layout.clear_data_bottom_sheet, null)
    dialog.setContentView(contentView)
    initview()

    //tomake background transparent
    try {
        context?.let { ContextCompat.getColor(it, android.R.color.transparent) }?.let { (contentView.parent as View).setBackgroundColor(it) }
    } catch (e: Exception) {
    }
}

private fun initview() {
}

}

1个回答

29

在 onStart 回调函数中调用此函数

override fun onStart() {
    super.onStart()
    dialog?.window?.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
}

4
先知们宣告了你的到来。 - Ricardo
1
通过使用<item name="android:backgroundDimEnabled">false</item>来设置BottomSheet主题的样式,可以实现相同的效果。 - Ryan Amaral

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接