如何更改 Android 警报对话框的背景? [英] How can I change the background of Android alert dialogs?
问题描述
我在我的应用程序中使用了 AlertDialog 类.默认情况下,这些警报对话框具有透明背景.我正在尝试使用不透明的背景,但非常不成功.这些是我的风格:
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:
<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>
我成功地为整个活动应用了MyOpaqueActivity"样式(窗口背景更改为my_background"),但它不适用于这些活动中的警报对话框.alertDialogStyle"属性和我的MyOpaqueAlertDialog"样式似乎没有任何效果.
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.
那么如何更改这些警报对话框的背景呢?
So how can I change the background of these alert dialogs?
推荐答案
你的方法行不通.似乎 AlertDialog(和 Builder)对主题进行了硬编码,并且在任何地方都不尊重 alertDialogStyle:
Your approach won't work. It seems AlertDialog (and Builder) hardcode the theme and don't honor alertDialogStyle anywhere:
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.
从 AlertDialog 派生的自定义对话框类调用受保护的构造函数 AlertDialog(context, R.style.MyOpaqueAlertDialog) 将是一种解决方法.
A custom dialog class derived from AlertDialog that calls the protected constructor AlertDialog(context, R.style.MyOpaqueAlertDialog) would be a workaround.
在最新的 android 源代码中,有一个新的用于 AlertDialog.Builder 的公共构造函数,它采用主题参数.不幸的是,它还没有发布(也许在 Gingerbread 中?).
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 警报对话框的背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!