简体   繁体   中英

Custom theme for AlertDialog not being applied

The common style for AlertDialogs is set in themes.xml

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

And the ThemeOverlay.AlertDialog looks like this:

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

However for one particular dialog in the app I need to use a different buttonStyle (textColor). For this I've created a style with a common style as a parent:

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

And then the AlertDialogButtonRed looks like:

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

And this is being set when creating a dialog:

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

Howevere the style seems not being applied, the button text color is not red. What am I missing?

Just set R.style.NewAlertButtonStyle in ContextThemeWrapper

You are speaking about the control buttons of the dialog itself, right? They are using android:buttonBarButtonStyle , which is the correct style for you to apply in the dialog overlay.

But you may probably already have changed the usage of your AlertDialog material dialogs of some sort. In that case there might be a different style you need to apply (there I'm not sure).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM