簡體   English   中英

為警報對話框設置 Android Theme.Light

[英]Set Android Theme.Light for Alert Dialog

我正在嘗試為我的警報對話框設置 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主題。 誰能告訴我為什么? 我哪里錯了?

編輯:如果您不知道我的問題的答案,請投票。 我有發布卡住問題的壞習慣:(

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

這也花了我一段時間才弄清楚。

手頭的問題是 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>

嘗試這個:

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

這就是我所做的。 它對我有用

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM