使用样式设置警示对话框的文本颜色

3
我刚刚更新了我的应用程序中的库版本,现在AlertDialog按钮的文本颜色是白色的。在那之前,如果我没记错的话,它们由 colorAccent 属性指定。我尝试了许多不同的属性,但似乎没有任何一个起作用。

示例照片 - 在右下角您可以看到几乎看不见的按钮:

enter image description here

我当前的警报对话框样式:

<style name="AlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorAccent">@color/website_main</item>
    <item name="android:textColor">@color/website_main</item>
</style>

在我的主题样式中:

<item name="android:alertDialogStyle">@style/AlertDialogStyle</item>

我正在使用的支持/设计/appcompat的当前版本是:25.1.0

以下是我创建AlertDialog的Java代码:

android.support.v7.app.AlertDialog.Builder alertDialogBuilder = new android.support.v7.app.AlertDialog.Builder(activity);
    alertDialogBuilder
            .setCancelable(false)
            .setPositiveButton(activity.getString(R.string.ok), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    <UnrelatedLogic...>
                }
            });
    return alertDialogBuilder.show();

这里我使用了一个ListPreference,它也使用了一个AlertDialog

android.support.v7.preference.ListPreference lang_preference = (android.support.v7.preference.ListPreference) findPreference("language_chooser");

有什么建议吗?

这是一个自定义对话框吗? - undefined
不,这只是一个普通的警告对话框,它也会在默认设置的对话框中出现。 - undefined
你能贴出显示这个AlertDialog的活动代码吗?这将有助于回答问题。 - undefined
好的,一会儿就可以完成。 - undefined
2个回答

8
这可能对您很有用。这是我的警告对话框样式:enter image description here
<style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="colorAccent">@color/teal</item>
        <item name="android:textColorPrimary">@color/bg_3</item>
        <item name="android:background">@color/bg_border</item>
    </style>

在 Activity/Adapter 中使用样式

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

问题在于这是一个特定的解决方案。在我的设置对话框中也发生了这种情况,所以解决方案必须是改变主应用程序的主题。 - undefined
这些更改仅适用于对话框,而不是整个应用程序。 - undefined
1
我正在面临同样的问题 - undefined

3
有点令人困惑,但是alertDialogStyle的作用是为Dialog框进行样式设置,而不是它所包含的任何子视图(尽管这似乎并不总是这样)。来自文档的说明如下:

当你把一个样式应用到布局中的单个View上时,样式定义的属性只会应用到该View。如果一个样式应用到了ViewGroup上,子View元素将不会继承样式属性,只有直接应用样式的元素会应用其属性。但是,你可以将一个样式作为主题应用,从而使其适用于所有View元素。

因此,必须将样式作为主题应用;同样也有一个相应属性:
<item name="android:alertDialogTheme">@style/AlertDialogStyle</item>

最后,textColor属性应该改变标题和正文文本的颜色,而不是按钮文本的文本颜色。你只需要应用colorAccent属性来给按钮文本上色即可:
<style name="AlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorAccent">@color/website_main</item>
</style>

@DannyHambourg 它有什么作用(如果有的话)?另外,你在哪个版本的Android上运行它?最后,尝试使用alertDialogTheme而不是android:alertDialogTheme;这可能与AppCompat有关。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接