【发布时间】:2018-01-03 23:58:35
【问题描述】:
我正在尝试为我的 android 应用创建一个深色主题,但带有浅色对话框。到目前为止,这一直是一场噩梦。
基本上,我的深色主题风格是这样的:
<style name="AppTheme.Dark" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/darkPrimary</item>
<item name="colorPrimaryDark">@color/darkPrimaryDark</item>
<item name="android:windowBackground">@color/darkPrimary</item>
<item name="android:textColorPrimary">@color/white</item>
<item name="android:textColorSecondary">@color/transparentWhite</item>
</style>
除了 AlertDialogs 之外,一切看起来都不错。 AlertDialogs 是白色的(应该是白色的),但我在 AlertDialogs 中的小部件(TextViews、EditTexts 等)也是白色的。我假设 AlertDialogs 正在使用我的主题的android:textColorPrimary 样式。那么如何才能为 AlertDialogs 指定不同的 android:textColorPrimary 呢?
我尝试将以下样式添加到我的主题中:
<item name="alertDialogTheme">@style/AlertDialogTheme</item>
然后是另一种风格:
<style name="AlertDialogTheme" parent="Base.Theme.AppCompat.Light.Dialog.Alert">
<item name="android:textColorPrimary">@color/black</item>
<item name="android:textColorSecondary">@color/black</item>
</style>
但它没有效果。我尝试了很多其他的东西,比如设置样式android:textAppearance、android:textViewStyle。我什么都做不了。必须有一些简单的方法将浅色主题仅应用于以黑色文本作为主要文本的对话框,同时在应用程序中保留白色主要文本。
编辑:所以我终于让事情按我想要的方式工作了,我会发布解决方案,以防其他人遇到同样的问题。我像@Jeffrey 建议的那样使用了 ContextThemeWrapper,但使用了 R.style.ThemeOverlay_AppCompat_Light。 但是,由于某种原因,这似乎不适用于我拥有自定义视图并拥有 EditTexts 的一个对话框。出于某种原因,EditTexts 没有被设置样式,所以我不得不将主题应用到自定义视图的布局 xml 中的根属性:
android:theme="@style/ThemeOverlay.AppCompat.Light"
【问题讨论】:
-
您的对话框主题使用了不同的父级 - 在规范中 - 这两个父级都指向同一个地方吗?
-
您的意思是删除“基础”。来自 AlertDialogTheme 父级?试过了,没用。
-
这就是我的意思。谢谢你。抱歉,没用。
标签: android