为警报对话框设置Android Theme.Light

问题描述 投票:27回答:5

我正在尝试为我的警报对话框设置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主题。谁能告诉我为什么?我哪里错了?

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

android android-layout alertdialog android-theme
5个回答
6
投票

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


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>

1
投票
parent="android:style/Theme.Light"

1
投票

试试这个:

<style name="popup_theme" parent="Theme.AppCompat.Light.Dialog.Alert">

0
投票

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

AlertDialog.Builder builder = new AlertDialog.Builder(context,R.style.Theme_AppCompat_Light_Dialog);
© www.soinside.com 2019 - 2024. All rights reserved.