27

我正在尝试为我的警报对话框设置 android Theme.Light主题,但到目前为止没有成功。在阅读了一些教程后,我收集到使用AlertDialog.Builder我们不能直接在构造函数中设置主题(至少在 API 级别 7 中)。

我发现的替代解决方案是使用ContextThemeWrapper,每个人都保证会解决我的问题。所以我编写了这样的代码:

AlertDialog.Builder builder = new AlertDialog.Builder(
                    new ContextThemeWrapper(context, R.style.popup_theme));

我在 values 文件夹中描述了我的主题:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="back_color">#ffffffff</color>
<style name="popup_theme" parent="@android:style/Theme.Light">
    <item name="android:windowBackground">@color/back_color</item>
    <item name="android:colorBackground">@color/back_color</item>
</style>

不幸的是,我仍然得到默认的Theme.Dialog.Alert主题。谁能告诉我为什么?我哪里错了?

编辑:如果您不知道我的问题的答案,请投票。我有发布卡住问题的坏习惯:(

4

5 回答 5

6

更改parent="android:Theme.Light"parent="@android:style/Theme.Light"

于 2011-07-18T07:28:20.290 回答
3

这也花了我一段时间才弄清楚。

手头的问题是 Theme.Light 和 Theme.Holo.Light 等都是围绕活动设计的。对话框主题需要基于诸如 @android:style/Theme.Dialog 之类的主题,其中包含特定于对话框的属性。

<style name="popup_theme" parent="@android:style/Theme.Dialog">

尝试使用以下内容覆盖 Theme.Dialog:

<item name="android:textAppearance">?android:attr/textAppearanceInverse</item>
于 2012-08-01T20:11:35.203 回答
2

试试这个:

<style name="popup_theme" parent="Theme.AppCompat.Light.Dialog.Alert">
于 2016-08-23T11:03:23.090 回答
1
parent="android:style/Theme.Light"
于 2011-11-10T04:40:30.343 回答
0

这就是我所做的。它对我有用

AlertDialog.Builder builder = new AlertDialog.Builder(context,R.style.Theme_AppCompat_Light_Dialog);
于 2017-12-29T17:14:16.637 回答