Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
206 views
in Technique[技术] by (71.8m points)

android - Button style in AlertDialogs

Does anyone know how to override the default style for AlertDialog buttons? I've looked through the Android source for themes and styles and experimented with different things but I haven't been able to find a way that works.

What I've got below works for changing the backgrounds, but doesn't do anything with the buttons. myTheme is applied to the whole <application> via the manifest. (Some other items were deleted for clarity, but they only relate to the title bar.)

<style name="myTheme" parent="android:Theme">
    <item name="android:buttonStyle">@style/customButtonStyle</item>
    <item name="android:alertDialogStyle">@style/dialogAlertTheme</item>
</style>

<style name="dialogAlertTheme" parent="@android:style/Theme.Dialog.Alert">
    <item name="android:fullDark">@drawable/dialog_loading_background</item>
    <item name="android:topDark">@drawable/dialog_alert_top</item>
    <item name="android:centerDark">@drawable/dialog_alert_center</item>
    <item name="android:bottomDark">@drawable/dialog_alert_bottom</item>
    <!-- this last line makes no difference to the buttons -->
    <item name="android:buttonStyle">@style/customButtonStyle</item> 
</style>

Any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Given that MrSnowflake's reasoning is inaccurate, I'm taking the freedom to provide another answer.

There is something wrong with the markup in Steve Haley's question, and it is mixing up alertDialogStyle and alertDialogTheme, most likely because of the fact that alertDialogTheme was introduced long after alertDialogStyle was around.

So even if @android:style/Theme.Dialog.Alert is available on your Andoid platform, you still can't utilize its expressional power by hooking a cusomized version back into your own theme unless your Android platform supports an android:alertDialogTheme attribute/item for themes. (It may or may not be the case that such inconsistent Android versions exist, I don't know for sure. But the markup used in the question suggests that it does.)

In the question's markup, the parent="@android:style/Theme.Dialog.Alert" will do nothing except creating the illusion that you're customizing the alert dialog theme when you're really only customizing the alert dialog style.

This is how the markup should look like; not all Android versions support all features.

<style name="myTheme" parent="android:Theme">
    <item name="android:buttonStyle">@style/customButtonStyle</item>
    <item name="android:alertDialogStyle">@style/dialogAlertStyle</item>
    <item name="android:alertDialogTheme">@style/dialogAlertTheme</item>
</style>

<style name="dialogAlertStyle" parent="@android:style/AlertDialog">
    <item name="android:fullDark">[...]</item>
    [...]
</style>

<style name="dialogAlertTheme" parent="@android:style/Theme.Dialog.Alert">
    <item name="android:windowBackground">[...]</item>
    [...]
</style>

Customizing the alert dialog style has been around for quite some time but is limited to providing (background) drawables for "fullDark", "topDark" etc.

Customizing the alert dialog theme opens a method to provide attributes such as windowBackground, windowTitleStyle and such, but as stated before, you need an Android version which supports the alertDialogThem attribute/item for themes. I can't figure out exactly when this was introduced but it hasn't been Android 2.2 and Eclipse will tell you anyway...

I don't have the resources to validate MrSnowflake's conclusion that it's impossible to style alert dialog buttons in XML, but unless we're facing one of those somewhat nasty aspects of Android where a feature is really missing, I find it unlikely.

As a matter of fact, what's missing in the question is the most relevant part in this respect, namely

<style name="customButtonStyle" />

so the conclusion that alert dialog buttons do not obey the Widget.Button is not yet proven from my point of view.

Consolidated conclusion: The abilities to style alert dialogs independently of other widgets is limited in Android but getting more powerful as new versions improve in this respect.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...