删除AlertDialog的暗淡持续时间

问题描述 投票:0回答:3

AlertDialog的暗淡效果可以通过以下方式禁用:

alertDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

此外,可以通过使用:来改变暗光量:

 alertDialog.getWindow().setDimAmount(0.0f);

但是,每当我调用这些函数时,更改似乎会在较短的时间内(100-200之间的较长值)发出动画效果。有没有办法使更改立即生效?

当尝试用模仿的View替换标准的暗淡时,由于暗淡变化的动画,屏幕似乎闪烁。

编辑:这是基本的应用程序主题:

<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>

AlertDialog是从AlertDialog.Builder创建的(单击侦听器已在以后添加):

    dialogBuilder=new AlertDialog.Builder(this);
    dialogBuilder.setTitle("Title").setIcon(null).setMessage("Description")
            .setNeutralButton("Neutral",null).setNegativeButton("Cancel",null).setPositiveButton("Ok",null);
    alertDialog=dialogBuilder.create();
java android-alertdialog
3个回答
0
投票

将此添加到您的主题:

        <item name="android:backgroundDimEnabled">false</item>

并严格通过代码控制主题

如果您不想通过主主题来控制对话框的主题仅为对话框创建一个子类,并通过以下方式使用它]

<style name="DialogTheme" parent="Theme.MaterialComponents.Light.NoActionBar.Bridge">
     <item name="android:backgroundDimEnabled">false</item>
</style>


dialogBuilder = new AlertDialog.Builder(this, R.style.DialogTheme);

0
投票

如果您希望所有对话框都禁用暗淡,则可以执行以下操作:转到styles.xml,然后在您的主主题中添加:

<item name="alertDialogTheme">@style/MyAlertDialogTheme</item>

然后在下面添加另一个主题(我们在上面引用的主题)

<style name="MyAlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
     <item name="android:backgroundDimEnabled">false</item>
</style>

此外,如果您想调低亮度,可以将其更改为:

<item name="android:backgroundDimAmount">0.05</item>

0
投票

正如这里的其他答案所建议的,我认为最好完全禁用暗淡效果。我不想覆盖所有AlertDialogs,因此仅使用背景View并使用AlertDialogsetOnShowListenersetOnDismissListener淡入/淡出它似乎就可以很好地工作。

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);
    }
});
© www.soinside.com 2019 - 2024. All rights reserved.