android - 如何设置 PreferenceFragmentCompat accentColor?

标签 android android-fragments android-theme android-attributes android-preference-v14

我想覆盖 v14 PreferenceFragmentCompat 使用的 accentColor。

我在我的 Android 应用的外框上使用了粉红色强调色。这在许多情况下都会产生问题,因为它会导致标准控件使用足够接近红色的强调色,以至于效果令人不安。不管怎样,我喜欢框架上有粉红色 FAB 和按钮控件的效果。

对于 child Activity ,我使用带有标准蓝绿色强调色的它们。但是,我在主要 Activity 的抽屉中有一个 PreferenceFragment compat,我无法弄清楚如何覆盖主要 Activity 的强调色。

我尝试过的方法(都不起作用):

在接收 PreferenceFragmentCompat 的 fragment 框架上设置主题(不起作用):

<FrameLayout android:id="@+id/preferenceFragmentFrame" 
    android:layout_width="match_parent" android:layout_height="0dp"
    android:layout_weight="1"
    android:theme="@style/AppTheme.TealAccentColor"
    />

AppTheme.TealAccentColor 样式提供明确的蓝绿色 acccentColor。

在偏好主题中设置 accentColor(不起作用):

  <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        ...
     <item name="preferenceTheme">@style/MyPreferenceThemeOverlay</item>
    </style>
    <style name=MyPreferenceThemeOverlay parent="PreferenceThemeOverlay.v14.Material>
         <item name="colorAccent">@color/colorAccentTeal</item>
    </style>

向 preference-v14 的 PreferenceThemeOverlay 添加强调色(不起作用):

  <!-- use the library's theme-->
  <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        ...
     <item name="preferenceTheme">PreferenceThemeOverlay.v14.Material</item>
   </style>
   <!-- but add an accentColor item to the library's theme -->
   <style name="PreferenceThemeOverlay.v14">
        <item name="colorAccent">@color/colorAccentTeal</item>
   </style>

无论我做什么,PreferenceFragmentCompat 似乎都采用了 Activity 主题中的粉色强调色。

我确定这与 Activity 的主题和 Fragment 的主题之间的脱节有关。但是 fragment 没有 xml 元素,因为 PreferenceFragmentCompat 提供了自己的布局。

也许有一种方法可以通过在扩展 PreferenceFragmentCompat 的类中进行重写以编程方式完成此操作,但如果有的话,我无法想象它会是什么。我能想到的大多数攻击点要么可以访问内部创建的布局,要么可以在布局创建后访问,但为时已晚。

图片可能有帮助:

enter image description here

最佳答案

您是否尝试过覆盖 android:colorAccent 而不是 colorAccent

使用您描述为“在首选项主题中设置 accentColor”的方法,只需更改属性名称即可。

偏好支持库根本没有考虑 appcompat,所以

  1. 忘记 appcompat 属性
  2. color* 属性只适用于 API 21,我认为

如果你想要一致的行为,你可以使用 my library Android Support Preference旨在连接首选项和 appcompat 支持库。

然后您使用 colorAccent(无前缀)的原始样式将按预期工作。

关于android - 如何设置 PreferenceFragmentCompat accentColor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33901470/

相关文章:

android - 如何使用 viewpager 和多个 fragment 创建多步骤注册表单?

java - Android - requestLocationUpdates 导致标识符预期错误

java - 无法解析符号 'Rounded Transformation'

android - Google Drive Android API ConnectionResult 错误

android - findViewById 在 fragment 中返回 null

Android 主题错误的

java - Android - 从 JSON 字符串转换为 Java 对象

android - 清空 Facebook FriendPickerFragment

android - 将 Android 工具栏弹出菜单主题从深色更改为浅色

java - 有没有办法找出这些自定义属性在 google iosched 应用程序中的作用?