Java 删除AlertDialog的dim持续时间
Java 删除AlertDialog的dim持续时间,java,android-alertdialog,Java,Android Alertdialog,AlertDialog的昏暗效果可以通过以下方式禁用: alertDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); 此外,可通过以下方式更改变暗量: alertDialog.getWindow().setDimAmount(0.0f); 然而,每当我调用这些函数时,变化似乎会在短时间内(100-200之间的长值)进行动画处理。有没有一种方法可以让改变瞬间发生 尝试用模拟视图替换标准dim时,
AlertDialog
的昏暗效果可以通过以下方式禁用:
alertDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
此外,可通过以下方式更改变暗量:
alertDialog.getWindow().setDimAmount(0.0f);
然而,每当我调用这些函数时,变化似乎会在短时间内(100-200之间的长值)进行动画处理。有没有一种方法可以让改变瞬间发生
尝试用模拟视图替换标准dim时,由于dim更改的动画,屏幕似乎会闪烁
编辑:以下是基本应用程序主题:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
将此添加到您的主题中:
<item name="android:backgroundDimEnabled">false</item>
false
并从代码上严格控制主题
如果不想通过主主题控制对话框的主题
仅为对话框创建一个子类,并按以下方式使用它
<style name="DialogTheme" parent="Theme.MaterialComponents.Light.NoActionBar.Bridge">
<item name="android:backgroundDimEnabled">false</item>
</style>
dialogBuilder = new AlertDialog.Builder(this, R.style.DialogTheme);
假的
dialogBuilder=新建AlertDialog.Builder(此为R.style.DialogTheme);
如果希望所有对话框都禁用dim,则可以执行以下操作:
转到styles.xml,然后在主主题中添加:
<item name="alertDialogTheme">@style/MyAlertDialogTheme</item>
@style/MyAlertDialogTheme
然后在下面添加另一个主题(我们在上面引用的主题)
假的
此外,如果您想要非常低的亮度,可以将其更改为:
<item name="android:backgroundDimAmount">0.05</item>
0.05
正如这里的其他答案所暗示的,我认为最好完全禁用暗淡效果。我不想覆盖所有的AlertDialog,所以它似乎可以很好地使用背景视图
,并使用AlertDialog
的setOnSowlistener
和setOnDismissListener
将其淡入/淡出
alertDialog=dialogBuilder.create();
alertDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
alertDialog.setOnShowListener(new DialogInterface.OnShowListener(){
@Override public void onShow(DialogInterface dialog) {
background.setAlpha(0f);background.setVisibility(View.VISIBLE);background.animate().alpha(1f).setDuration(dur);
}
});
alertDialog.setOnDismissListener(new DialogInterface.OnDismissListener(){
@Override public void onDismiss(DialogInterface dialog){
background.animate().alpha(0f).withEndAction(runnable_background_gone);
}
});
请发布警报对话框主题或应用程序主题或both@LenaBru您好,我已经添加了应用程序主题和创建AlertDialog的代码,但我不确定如何获取AlertDialog主题。AlertDialog是动态创建的,不是用xml创建的。this=使用上下文的主题this感谢您的建议。我尝试添加您提到的子类样式,但当我打开它(
alertDialog.show()
)时,由于某种原因,它覆盖了整个应用程序屏幕。请参阅更新的答案尝试继承自Theme.MaterialComponents.Light.NoActionBar.Bridge而不是您的应用程序主题。很遗憾,我遇到了同样的问题。Ofek您好,当我将@style/MyAlertDialogTheme
添加到应用程序主题时,对话框大小正常,但当我执行alertDialog.show()时,后面的所有内容都变为黑色。
<item name="android:backgroundDimAmount">0.05</item>
alertDialog=dialogBuilder.create();
alertDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
alertDialog.setOnShowListener(new DialogInterface.OnShowListener(){
@Override public void onShow(DialogInterface dialog) {
background.setAlpha(0f);background.setVisibility(View.VISIBLE);background.animate().alpha(1f).setDuration(dur);
}
});
alertDialog.setOnDismissListener(new DialogInterface.OnDismissListener(){
@Override public void onDismiss(DialogInterface dialog){
background.animate().alpha(0f).withEndAction(runnable_background_gone);
}
});