AlertDialog 使用什么主题自动调整颜色 day/night 主题

what theme to use for the AlertDialog to adjust the color automatically with day/night theme

如果调用 AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); 的应用程序应该显示深色主题,如果不是,则应用程序使用浅色主题。

拥有 AlertDialog.Builder(this),并希望应用一个主题,以便在 MODE_NIGHT 中显示深色主题的对话框,否则对话框显示浅色主题,如下所示(但是此 android.R.style.Theme_Material_Dialog 将导致对话框始终处于深色主题)

AlertDialog.Builder(this, android.R.style.Theme_Material_Dialog)

AlertDialog 有一个主题吗?还是必须定义两个主题并检查模式,然后分别用主题编码?

styles.xml

中定义您的警报对话框
<style name="MyDialogStyle" parent="Theme.AppCompat.DayNight.Dialog.Alert"/>

在你的代码中

AlertDialog.Builder(this, R.style.MyDialogStyle)

并试一试!

添加到您的主题样式:

        <item name="alertDialogTheme">@style/Theme.AppCompat.DayNight.Dialog.Alert</item>
        <item name="android:alertDialogTheme">@style/Theme.AppCompat.DayNight.Dialog.Alert</item>

使用来自 appcompat 的对话框

import androidx.appcompat.app.AlertDialog;

AlertDialog alertDialog = new AlertDialog.Builder(context).create()