如何更改 Android 警报对话框的背景?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3118601/
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
How can I change the background of Android alert dialogs?
提问by Alpha Hydrae
I use the AlertDialog class in my application. By default, these alert dialogs have a transparent background. I'm trying to use an opaque background instead, very unsuccessfully. These are my styles:
我在我的应用程序中使用 AlertDialog 类。默认情况下,这些警报对话框具有透明背景。我正在尝试使用不透明的背景,但非常不成功。这些是我的风格:
<style name="MyOpaqueActivity" parent="@android:style/Theme.Dialog">
<item name="android:windowBackground">@drawable/my_background</item>
<item name="android:alertDialogStyle">@style/MyOpaqueAlertDialog</item>
</style>
<style name="MyOpaqueAlertDialog" parent="@android:style/Theme.Dialog.Alert">
<item name="android:background">#454545</item>
<item name="android:windowBackground">@drawable/my_background</item>
<item name="android:popupBackground">@drawable/my_background</item>
</style>
I applied the "MyOpaqueActivity" style successfully for whole activities (the window background is changed to "my_background"), but it doesn't work for alert dialogs within those activities. The "alertDialogStyle" attribute and my "MyOpaqueAlertDialog" style don't seem to have any effect.
我为整个活动成功应用了“MyOpaqueActivity”样式(窗口背景更改为“my_background”),但它不适用于这些活动中的警报对话框。“alertDialogStyle”属性和我的“MyOpaqueAlertDialog”样式似乎没有任何效果。
So how can I change the background of these alert dialogs?
那么如何更改这些警报对话框的背景呢?
采纳答案by kipkennedy
Your approach won't work. It seems AlertDialog (and Builder) hardcode the theme and don't honor alertDialogStyle anywhere:
你的方法行不通。似乎 AlertDialog(和 Builder)对主题进行了硬编码,并且在任何地方都不尊重 alertDialogStyle:
protected AlertDialog(Context context) {
this(context, com.android.internal.R.style.Theme_Dialog_Alert);
}
public Builder(Context context) {
this(context, com.android.internal.R.style.Theme_Dialog_Alert);
}
They came to the same conclusion here.
他们在这里得出了同样的结论。
A custom dialog class derived from AlertDialog that calls the protected constructor AlertDialog(context, R.style.MyOpaqueAlertDialog) would be a workaround.
从 AlertDialog 派生的自定义对话框类调用受保护的构造函数 AlertDialog(context, R.style.MyOpaqueAlertDialog) 将是一种解决方法。
In the latest android source, there is a new public constructor for AlertDialog.Builder that takes a theme argument. Unfortunately, it hasn't been released yet (maybe in Gingerbread?).
在最新的 android 源代码中,AlertDialog.Builder 有一个新的公共构造函数,它接受一个主题参数。不幸的是,它还没有发布(也许在 Gingerbread 中?)。
回答by class stacker
It's easy to do this in an XML style once you figure that AlertDialogs have somewhat special attributes.
一旦您发现 AlertDialogs 有一些特殊的属性,就很容易以 XML 样式执行此操作。
However, it would be interesting to know which Android version you're referring to (or are targeting at). Theme.Dialog is relatively new as far as I know. Newer Android versions also don't use AlertDialogs under all circumstances where older versions used them.
但是,知道您指的是(或针对的)Android 版本会很有趣。据我所知,Theme.Dialog 相对较新。在旧版本使用它们的所有情况下,较新的 Android 版本也不使用 AlertDialogs。
This works for me in Android 2.2:
这在 Android 2.2 中对我有用:
<style name="MyTheme" parent="@android:style/Theme">
<item name="android:alertDialogStyle">@style/MyOpaqueAlertDialog</item>
</style>
<style name="MyOpaqueAlertDialog">
<item name="android:fullDark">@drawable/[...]</item>
<item name="android:topDark">@drawable/[...]</item>
<item name="android:centerDark">@drawable/[...]</item>
<item name="android:bottomDark">@drawable/[...]</item>
<item name="android:fullBright">@drawable/[...]</item>
<item name="android:topBright">@drawable/[...]</item>
<item name="android:centerBright">@drawable/[...]</item>
<item name="android:bottomBright">@drawable/[...]</item>
<item name="android:bottomMedium">@drawable/[...]</item>
<item name="android:centerMedium">@drawable/[...]</item>
</style>
Newer Android versions also have android:progressLayout and android:horizontalProgressLayout attributes for the AlertDialog style.
较新的 Android 版本还具有 AlertDialog 样式的 android:progressLayout 和 android:horizontalProgressLayout 属性。
Also, in newer Android versions, it is possible to refer to an AlertDialog theme by using alertDialogTheme instead of alertDialogStyle in a custom theme. AlertDDialogThemes support more familiar and more powerful attributes like windowBackground, windowTitleStyle etc. Have a look at styles.xml and themes.xml.
此外,在较新的 Android 版本中,可以通过在自定义主题中使用 alertDialogTheme 而不是 alertDialogStyle 来引用 AlertDialog 主题。AlertDDialogThemes 支持更熟悉和更强大的属性,如 windowBackground、windowTitleStyle 等。看看styles.xml 和themes.xml。
Unfortunately, the documentation of which feature was added when is super poor. Please find out for yourself whcih approach is the best for you.
不幸的是,关于何时添加哪个功能的文档非常糟糕。请自己找出哪种方法最适合您。
回答by Robby Pond
From the documentationits looks like you could do this in onCreateDialog.
从文档看来,您可以在 onCreateDialog 中执行此操作。
FrameLayout fl = (FrameLayout) findViewById(android.R.id.custom);
fl.setBackgroundResource(); // has to be a drawable.
Only other solution is a custom dialog using that Theme.
只有其他解决方案是使用该主题的自定义对话框。