AlertDialog使用什么主题来与日/夜主题自动调整颜色

lan*_*nyf 2 android themes android-alertdialog

如果调用的应用程序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)
Run Code Online (Sandbox Code Playgroud)

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

Nhấ*_*ang 5

在中定义警报对话框 styles.xml

<style name="MyDialogStyle" parent="Theme.AppCompat.DayNight.Dialog.Alert"/>
Run Code Online (Sandbox Code Playgroud)

在你的代码中

AlertDialog.Builder(this, R.style.MyDialogStyle)
Run Code Online (Sandbox Code Playgroud)

并尝试一下!