0

AlertDialogs 的常用样式设置在themes.xml

<style name="CustomTheme" parent="Theme.AppCompat.Light.NoActionBar">
    ...
    <item name="alertDialogTheme">@style/ThemeOverlay.AlertDialog</item>
</style>

ThemeOverlay.AlertDialog看起来像这样:

<style name="ThemeOverlay.AlertDialog" parent="ThemeOverlay.AppCompat.Dialog.Alert">
    ...
    <item name="buttonStyle">@style/AlertDialogButton</item>
    ...
</style>

但是对于应用程序中的一个特定对话框,我需要使用不同的buttonStyle(textColor)。为此,我创建了一种具有通用样式的样式作为父级:

<style name="NewAlertButtonStyle" parent="ThemeOverlay.AlertDialog">
    <item name="buttonStyle">@style/AlertDialogButtonRed</item>
</style>

然后AlertDialogButtonRed看起来像:

<style name="AlertDialogButtonRed" parent="Widget.AppCompat.Button">
    <item name="android:textColor">@color/red</item>
</style>

这是在创建对话框时设置的:

AlertDialog.Builder(ContextThemeWrapper(context, R.style.NewAlertButtonStyle))
                .setTitle(getString(R.string.dialog_title))
                .setMessage(getString(R.dialog_body))
                .setPositiveButton(R.string.ok) { _, _ ->
                    //doing something here
                }.show()

然而,样式似乎没有被应用,按钮文本颜色不是红色。我错过了什么?

4

1 回答 1

0

刚入R.style.NewAlertButtonStyle_ContextThemeWrapper

于 2020-04-28T10:40:55.377 回答