华为手机dialog叠加时,弹出第二个dialog时会有背景闪烁
闪烁的主要原因是BottomSheetDialog的默认样式,它定义了默认dim行为.
我们可以通过使用bottomSheetDialogTheme定义自定义主题来解决上述问题:
backgroundDimEnabled :是否允许对话框变暗
<item name="android:backgroundDimEnabled">false</item>
以上是在网上找的方法非常开心以为解决了问题,结果QA同学发现需要遮罩的地方也没有遮罩了
通过查看源码得出的动态设置遮罩的方法
默认不需要遮罩:
<item name="android:backgroundDimEnabled">false</item>
代码里动态添加遮罩:
java代码:
public void setDim(Dialog dialog) {
Window window = dialog.getWindow();
if (window != null) {
WindowManager.LayoutParams layoutParams = window.getAttributes();
if (layoutParams == null) {
layoutParams = new WindowManager.LayoutParams();
}
layoutParams.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;
window.setDimAmount(0.6f);
window.setAttributes(layoutParams);
}
}
kotlin代码:
fun setDim() {
val window = this.window
if (window != null) {
var params = window.attributes
if (params == null) {
params = WindowManager.LayoutParams()
}
params.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND
window.setDimAmount(0.6f)
window.attributes = params
}
}
默认需要遮罩:
<item name="android:backgroundDimEnabled">false</item>
在代码里动态移除遮罩
dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);