【发布时间】:2017-03-12 11:38:25
【问题描述】:
我正在努力更改 AppCompat DialogFragments 的 TEXT 颜色。
我的应用程序使用 DARK 主题 (Theme.AppCompat.NoActionBar),但对于对话框,我想要 LIGHT 主题。我正在使用构建工具、支持库和 compileSdkVersion 到 25,这有关系吗?
我可以更改对话框上的所有其他内容(标题、背景、窗口背景)但不能更改主要和重音文本颜色,继续使用深色主题的(白色)设置,导致白色背景上的白色文本。
我在 SO 的类似问题中尝试了数十种解决方案,例如:
1) 最简单的:在styles.xml上:
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="alertDialogTheme">@style/AppCompatAlertDialogStyle</item>
<item name="android:alertDialogTheme">@style/AppCompatAlertDialogStyle</item>
</style>
<style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<!-- ignored !!!! -->
<item name="colorPrimary">#ff0000</item>
<!-- ignored !!!! -->
<item name="colorPrimaryDark">#ff0000</item>
<!-- ignored !!!! -->
<item name="colorAccent">#ff0000</item>
<!-- ignored !!!! -->
<item name="android:textColorPrimary">#F040FF</item>
<!-- ignored !!!! -->
<item name="android:textColor">#F040FF</item>
</style>
使用此解决方案,将应用 AppCompat.Light.Dialog.Alert 的背景和按钮样式,但不会应用屏幕截图中的文本颜色:
2) 在 AlertDialog Creation 上手动指定样式:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.AppCompatAlertDialogStyle);
LayoutInflater inflater = getActivity().getLayoutInflater();
View hostView = mHostView = inflater.inflate(layoutId, null);
同样的问题。浅色背景,浅色文字。
3) 使用 ContextWrapper:
ContextThemeWrapper ctw = new ContextThemeWrapper(getActivity(), R.style.AppCompatAlertDialogStyle);
AlertDialog.Builder builder = new AlertDialog.Builder(ctw);
什么都没有:(同样的事情发生
4) 手动指定其他奇异常量我在 SO 中遇到了许多帖子,例如
Theme_DeviceDefault_Light_Dialog_Alert
THEME_DEVICE_DEFAULT_LIGHT
这只是一个绝望的尝试,但无论如何文本没有改变
5) 在片段中而不是在对话框中指定样式
Dialog_Meta newFragment = new Dialog_Meta();
newFragment.setStyle(DialogFragment.STYLE_NORMAL, R.style.AppCompatAlertDialogStyle);
newFragment.show(fragmentManager, TAG);
我以前在一个非常旧的 API 版本中使用过这个解决方案,不记得是什么问题,但无论如何,并没有解决当前的问题:(
谁能告诉我这是怎么回事?
【问题讨论】:
-
只是为了确定:您使用的是
android.support.v7.app.AlertDialog,而不是android.app.AlertDialog,是吗? -
感谢您的提示。是的,我正在使用 v7.app.AlertDialog !
-
哦,好吧,我想我明白原因了。您在
AlertDialog上设置了自己的View,因此该主题不适用于Views。即兴发挥,您也许可以将 #2 和 #3 结合起来,得到LayoutInflater.from()和ContextThemeWrapper,而不是getActivity().getLayoutInflater()。 -
嗯,我什么都想不出来。在我有机会整理答案之前还需要一点时间,所以我会在此期间考虑一下。
-
是啊!!!!!!!如此简单,如此优雅!这是最好的解决方案。谢谢一百万,先生。
标签: android material-design android-appcompat android-theme