【问题标题】:Custom theme for AlertDialog not being applied未应用 AlertDialog 的自定义主题
【发布时间】:2020-08-12 03:03:32
【问题描述】:

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

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

【问题讨论】:

  • 你不应该在 ContextThemeWrapper 中设置 R.style.NewAlertButtonStyle 吗?
  • 对不起,我的错,对,我实际上在 ContextThemeWrapper 中使用NewAlertButtonStyle 并在问题中输入了错误的样式。现在将编辑它

标签: android styles android-alertdialog android-dialog app-themes


【解决方案1】:

只需将R.style.NewAlertButtonStyle 设置为ContextThemeWrapper

【讨论】:

  • 对不起,我把问题写错了,现在编辑。我实际上在ContextThemeWrapper 中使用R.style.NewAlertButtonStyle 但这仍然不起作用
  • 请在您的代码中添加一些解释,以便其他人可以从中学习
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-28
  • 1970-01-01
相关资源
最近更新 更多