【问题标题】:Android PreferenceFragment title text colorAndroid PreferenceFragment 标题文本颜色
【发布时间】:2016-05-18 09:39:24
【问题描述】:

我正在使用 AppCompatPreferenceActivity,它为手机使用 PreferenceActivity 简化 UI(无片段),但当它是平板电脑时使用带有 PreferenceFragment 的两窗格 UI。

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

我现在正在实现一个深色主题,所以这一次,我的主题的父主题是“Theme.AppCompat”。

它看起来像这样:

如您所见,首选项片段的标题是深灰色背景上的黑色。如何设置这个“标题”颜色?

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

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

...但它不再适用于 Lollipop 和 Marshmallow。

有什么想法吗?

【问题讨论】:

    标签: android android-preferences android-theme


    【解决方案1】:

    通过查看 PreferenceActivity 代码,我最终发现右侧窗格中的标题不是偏好片段的一部分,而是片段“面包屑”。

    通过使用此关键字(“面包屑”)搜索答案,我看到有人已经问过同样的问题,并且接受的答案很好,详细说明了如何做到这一点,以及为什么它比只是改变样式中的颜色。

    问题来了:Changing the highlight and title color for fragments in PreferenceActivity

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

    【讨论】:

      【解决方案2】:

      你试过这样吗?

      在styles.xml中

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

      在清单中

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

      【讨论】:

        【解决方案3】:

        我通过覆盖类别主题对其进行了更改。

        <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>
        

        layout/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

        【讨论】:

        • 谢谢。编译时收到以下消息:“无法解析符号'Preference.Category'”。
        • 确保您使用的是最新版本的v7 lib
        • 我没有使用支持首选项类,而是使用“普通”类,并且我在我的 -v21 样式文件中,因为我描述的问题仅适用于 API 21 和更高版本。在 API 21 之前没有问题。
        • 我删除了 @style/CategoryStyle 这一行,因为它适用于 API 21,它只适用于“android:...”这一行。但它不起作用,文本仍然是黑色的。有什么想法吗?
        猜你喜欢
        • 1970-01-01
        • 2013-06-02
        • 2011-10-05
        • 2017-12-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多