我正在努力改变AppCompat DialogFragments的文本颜色。
我的应用程序使用DARK主题(Theme.AppCompat.NoActionBar
),但对于对话框,我想要一个LIGHT主题。我使用Build Tools、Support Library和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时手动指定样式:
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
,对吧? - Mike M.