【发布时间】: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