Android AlertDialogs 中的按钮样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2524051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:10:34  来源:igfitidea点击:

Button style in AlertDialogs

androidandroid-alertdialog

提问by Steve Haley

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

有谁知道如何覆盖 AlertDialog 按钮的默认样式?我已经查看了主题样式的 Android 源代码并尝试了不同的东西,但我一直无法找到一种有效的方法。

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

我下面的内容可用于更改背景,但对按钮没​​有任何作用。通过清单myTheme应用于整体<application>。(为清楚起见,删除了其他一些项目,但它们仅与标题栏有关。)

<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?

有任何想法吗?

采纳答案by MrSnowflake

See all other anwsers below.

请参阅下面的所有其他答案。

Incorrect:

不正确:

I guess you have to implement your own Dialogclass.

我想你必须实现自己的Dialog类。

回答by class stacker

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

鉴于 MrSnowflake 的推理是不准确的,我冒昧地提供另一个答案。

There is something wrongwith the markupin 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.

也有一些是错误标记史蒂夫·海利的问题,它是混合了alertDialogStyle和alertDialogTheme,很可能是因为那alertDialogTheme引入长alertDialogStyle大约是在事后。

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:alertDialogThemeattribute/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.)

因此,即使 @android:style/Theme.Dialog.Alert 在您的 Andoid 平台上可用,除非您的 Android 平台支持android:alertDialogTheme属性,否则您仍然无法通过将自定义版本挂钩回您自己的主题来利用其表达能力/主题项目。(这种不一致的Android版本可能存在也可能不存在,我不确定。但问题中使用的标记表明确实如此。)

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

在问题的标记中, parent="@android:style/Theme.Dialog.Alert"不会做任何事情,除了创建您正在自定义警报对话框主题的错觉,而实际上您只是在自定义警报对话框样式。

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

这就是标记的样子;并非所有 Android 版本都支持所有功能。

<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.

自定义警报对话框样式已经有一段时间了,但仅限于为“fullDark”、“topDark”等提供(背景)可绘制对象。

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...

自定义警报对话框主题会打开一个方法来提供诸如 windowBackground、windowTitleStyle 等属性,但如前所述,您需要一个支持主题的 alertDialogThem 属性/项的 Android 版本。我不知道这是什么时候引入的,但它不是 Android 2.2 并且 Eclipse 无论如何都会告诉你......

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.

我没有足够的资源来验证 MrSnowflake 的结论,即不可能在 XML 中设置警报对话框按钮的样式,但除非我们面临 Android 中确实缺少某个功能的那些有点令人讨厌的方面之一,否则我认为这不太可能。

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.

因此,从我的角度来看,警报对话框按钮不服从 Widget.Button 的结论尚未得到证实。

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.

综合结论:独立于其他小部件的警报对话框样式能力在 Android 中受到限制,但随着新版本在这方面的改进而变得更加强大。

回答by Witoldio

Try this:

尝试这个:

 <item name="buttonBarStyle">@style/Widget.AppCompat.ButtonBar</item>
    <item name="buttonBarButtonStyle">@style/AppTheme.Button</item>
    <item name="buttonBarPositiveButtonStyle">@style/YoursTheme</item>
    <item name="buttonBarNegativeButtonStyle">@style/YoursTheme</item>
    <item name="buttonBarNeutralButtonStyle">@style/YoursTheme</item>

回答by miracula

You can override "buttonBarButtonStyle" instead of "buttonStyle" like this:

您可以像这样覆盖“buttonBarButtonStyle”而不是“buttonStyle”:

<style name="dialogAlertTheme" parent="@android:style/Theme.Dialog.Alert">
  <item name="android:buttonBarButtonStyle">@style/customButtonStyle</item>
</style>

回答by Peter Keefe

If you're using Material Components:

如果您使用的是材料组件:

<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:alertDialogTheme">@style/AlertDialogTheme.Light</item>
</style>

<style name="AlertDialogTheme.Light" parent="ThemeOverlay.MaterialComponents.Dialog.Alert" >
    <item name="android:buttonBarButtonStyle">@style/InsertCustomButtonStyleHere</item>
</style>

This will keep your theme's colors but replace the style of the buttons, for instance to match Widget.MaterialComponents.Button.TextButton.

这将保留您主题的颜色,但会替换按钮的样式,例如匹配Widget.MaterialComponents.Button.TextButton.

回答by Gustavo Baiocchi Costa

I made a method like this that will change the default button colors and background

我做了一个这样的方法来改变默认的按钮颜色和背景

public static void setDefaultColorTextForDialogButton(AlertDialog alertDialog, Resources r) {
    Button b;

    b= alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE);
    if(b != null) {
        b.setTextColor(r.getColor(R.color.default_text));
        b.setBackgroundColor(Color.WHITE);
    }
    b = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
    if(b != null) {
        b.setTextColor(r.getColor(R.color.default_text));
        b.setBackgroundColor(Color.WHITE);
    }
    b = alertDialog.getButton(DialogInterface.BUTTON_NEUTRAL);
    if(b != null) {
        b.setTextColor(r.getColor(R.color.default_text));
        b.setBackgroundColor(Color.WHITE);
    }
}

This could be useful for some people!!!

这可能对某些人有用!!!