【问题标题】:How to apply a style/theme (material) to a preference in androidX?如何将样式/主题(材料)应用于 androidX 中的首选项?
【发布时间】:2019-06-27 12:11:01
【问题描述】:

我知道这可能是重复的,比如

Applying AppCompat theme to individual preferences in a PreferenceFragment

我在 youtube 上使用了这个视频: https://www.youtube.com/watch?v=PS9jhuHECEQ

从 devolper android 这个指南: https://developer.android.com/guide/topics/ui/settings

建立我的偏好。目前我的偏好库是:

实现 'androidx.preference:preference:1.1.0-beta01'

所以现在我想将我的 apptheme 应用到我的偏好设置...

我的 manifest.xml 中有这个

    <application
        android:name=".XXXApplication"
        android:allowBackup="false"
        android:label="@string/W_APP_NAME"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:ignore="GoogleAppIndexingWarning">

        <activity
            android:name=".Views.MainActivity"
            android:label="@string/W_APP_NAME"
            android:screenOrientation="landscape"
            android:theme="@style/AppTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

我的样式.xml

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

        <item name="preferenceTheme">@style/PreferenceThemeOverlay</item>

        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

现在我有一个 root_preferences.xml,其中包含其他几个首选项,如下所示:

root_preferences.xml

<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <Preference
            app:title="@string/N_CAM_ITEM"
            app:summary="@string/P_CAMERA_SETTINGS"
            app:icon="@drawable/ic_menu_camera"
            app:fragment="com.example.XXX.Views.CameraSettingFragment"/>

还有一个 camera_preferences.xml 也是一个 PreferenceScreen 包含 SwitchPreferences 等等......

如果我导航到设置我的SettingsFragment 被调用..

    public class SettingsFragment extends PreferenceFragmentCompat {

    @Override
    public void onCreatePreferences(@Nullable Bundle savedInstanceState, @Nullable String rootKey) {
        setPreferencesFromResource(R.xml.root_preferences, rootKey);

    }

那么我错过了什么,我的主题/风格不适用于这些偏好?希望你能帮我解决这个问题!

更新 我的设置看起来如何

所以复选框和开关没有显示任何颜色...一定有一些主要的橙绿色

【问题讨论】:

  • Preference API 非常糟糕,我强烈建议查看Preference Fix Library,它处理了很多问题,包括缺少材料主题。
  • 可能是它被破坏了,但是如果我看一下这个官方偏好示例:github.com/googlesamples/android-preferences apptheme 样式却被应用了
  • “我的主题/风格不适用于这些偏好”是什么意思? - 你能附上截图吗?此外,关于“缺少材料主题”不应该有任何问题 - 自从创建您链接的修复库以来,已经修复了许多错误。如果您特别注意到某些问题,可以提交错误here
  • 我已经用屏幕截图更新了我的问题 - 这种行为也出现在早期版本中......
  • 您是否使用 AppCompatActivity 作为您的基本活动?你的 colorAccent 设置是什么?

标签: android material-design android-theme android-preferences androidx


【解决方案1】:

正如@Louis 在 cmets 中指出的那样,ColorAccent 是在首选项设置中实现彩色元素的方法。非常感谢路易斯!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-10
    • 2020-10-26
    • 2021-02-21
    • 1970-01-01
    • 1970-01-01
    • 2020-07-05
    • 2021-12-01
    • 1970-01-01
    相关资源
    最近更新 更多