Android PreferenceFragment标题文字颜色

时间:2016-05-18 09:39:24

标签: android android-preferences android-theme

我使用的是AppCompatPreferenceActivity,它为手机使用了PreferenceActivity简化的UI(没有片段),但是当它是平板电脑时,它是一个带有PreferenceFragment的双窗格UI。

到目前为止,我只使用了一个轻量级主题(我的主题是父主题是Theme.AppCompat.Light)并没有问题,在双窗格模式下看起来像这样:

enter image description here

我现在正在实施一个黑暗的主题,所以这一次,我的主题的父母是" Theme.AppCompat"。

它看起来像这样:

enter image description here

如您所见,偏好片段的标题在深灰色背景上为黑色。我怎样才能设置这个" title"颜色?

注意:在Pre-Lollipop上,这很容易,我们只需要将其添加到偏好活动主题:

<item name="android:textColorSecondary">#AAAAAA</item>

...但它在Lollipop和Marshmallow上不再有效。

有什么想法吗?

3 个答案:

答案 0 :(得分:1)

通过检查PreferenceActivity代码,我最终发现右窗格中的标题不是首选项片段的一部分,而是片段“breadcrumb”。

通过使用此关键字(“breadcrumb”)搜索答案,我看到有人已经问了同样的事情,并且接受的答案很棒并且详细说明了如何做,以及为什么它比只是改变一种风格的颜色。

以下是问题:Changing the highlight and title color for fragments in PreferenceActivity

与答案的直接链接:https://stackoverflow.com/a/27078485/1534408

答案 1 :(得分:0)

我通过重写类别主题来改变它。

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
    //other stuff
    <item name="preferenceTheme">@style/MyPrefTheme</item>
</style>

<style name="MyPrefTheme" parent="@style/PreferenceThemeOverlay">
    <item name="preferenceCategoryStyle">@style/CategoryStyle</item>
    <item name="android:preferenceCategoryStyle">@style/CategoryStyle</item>
</style>

<style name="CategoryStyle" parent="Preference.Category">
    <item name="android:layout">@layout/category_view</item>
</style>

<强>布局/ category_view.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@android:id/title"
          style="?android:attr/listSeparatorTextViewStyle"
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:textColor="@color/white"
          android:layout_height="wrap_content"
    />

了解更多访问Preference style.xml

答案 2 :(得分:0)

你有没有试过这个?

style.xml中的

<style name="PreferenceScreen" parent="YourApplicationThemeOrNone">
    <item name="android:textColor">@color/TitleColor</item>
</style>
中的

<activity
      android:name="MyPreferenceActivity"
      ...
      android:theme="@style/PreferenceScreen" >
</activity>