首页 > 解决方案 > 警报对话框主题不从活动主题继承`windowBackground`

问题描述

在我的应用程序中,我有这个主题styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">#000000</item>
    <item name="colorAccent">@color/neon_green</item>
    <item name="android:windowBackground">@color/black_17</item>
</style>

但该属性windowBackground不会导致对话框背景发生变化。对话框背景颜色仍然是白色。但是,对话框文本的neon_green突出显示颜色确实出现了。

只有不改变的背景颜色。

AlertDialog.Builder builder = 
        new AlertDialog.Builder(new ContextThemeWrapper(act, R.style.AppTheme));
builder.setMessage("text")
        .setPositiveButton("pos ", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                // FIRE ZE MISSILES!
            }
        })
        .setNegativeButton("neg", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                // User cancelled the dialog
            }
        });
builder.create().show();

标签: android

解决方案


我刚刚将它添加到我的样式中

<style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:textColor">@color/white</item>
    <item name="android:textColorPrimary">@color/white</item>
    <item name="android:background">@color/black_17</item>
</style>

然后我将样式传递给构建器

AlertDialog.Builder builder =
                new AlertDialog.Builder(act,
                           R.style.AppCompatAlertDialogStyle);

推荐阅读