使用 BottomSheetDialogFragment 删除底表中的昏暗背景

Ank*_*oor 3 android

我正在使用 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() {
}
Run Code Online (Sandbox Code Playgroud)

}

use*_*210 12

在 onStart 回调中调用它

override fun onStart() {
    super.onStart()
    dialog?.window?.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
}
Run Code Online (Sandbox Code Playgroud)

  • 先知宣布了你的到来。 (4认同)
  • 相当于使用 `&lt;item name="android:backgroundDimEnabled"&gt;false&lt;/item&gt;` 设置 BottomSheet 主题的样式 (2认同)