使用样式的警报对话框文本颜色

Alert dialog text color using styles

我刚刚更新了应用程序中的库版本,现在 AlertDialog 按钮的文本颜色为白色。在此之前,如果我没记错的话,是 colorAccent 属性指定它们。我尝试了许多不同的属性,其中 none 似乎有效。

示例照片 - 在右下角您可以看到非常隐蔽的按钮:

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

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

我使用的 support/design/appcompat 当前版本是:25.1.0

Java 我在其中创建 AlertDialog:

的代码
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");

有什么建议吗?

可能 you.This 的用法是我的 警报对话框样式:

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

有点令人困惑,但是 alertDialogStyle 是用来设置 Dialog 框的样式,而不是它包含的任何 child 视图(尽管,这显然并非总是如此) .来自 documentation:

When you apply a style to a single View in the layout, the properties defined by the style are applied only to that View. If a style is applied to a ViewGroup, the child View elements will not inherit the style properties—only the element to which you directly apply the style will apply its properties. However, you can apply a style so that it applies to all View elements—by applying the style as a theme.

因此,您必须将样式应用为主题;其中还有一个属性:

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

最后,textColor 属性 应该 更改标题和 body 文本的颜色,而不是按钮的文本颜色。您应该只需要应用 colorAccent 属性来为按钮文本着色:

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