java - 如何将主题应用到首选项屏幕

标签 java android themes preferencefragment

我正在 viewpager 中创建 PreferenceFragment 并收到错误

java.lang.IllegalStateException: Must specify preferenceTheme in theme

我提到了PreferenceFragmentCompat requires preferenceTheme to be set但没能解决。我添加了 styles.xml,如下所示。

<resources>

    <!-- 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="android:preferenceStyle">@style/PreferenceThemeOverlay</item>
    </style>

    <style name="SettingsTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
    </style>
</resources>

下面是 list 代码

  <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity android:name=".MainActivity"
            android:theme="@style/Theme.AppCompat.DayNight.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

以下是首选项屏幕的代码

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:key="screen">

    <SwitchPreference
        android:key="sc"
        android:summary="Hi"
        android:title="Hello" />

</PreferenceScreen>

你能给我建议吗,我第一次使用主题,并对它感到困惑。仍然遇到同样的错误。

最佳答案

manifest 中的 Activity 标记中尝试此操作:

<activity android:name=".MainActivity"
        android:theme="@style/AppTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

您加载首选项 fragment activity没有设置主题。

同时在您的Apptheme中进行设置

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

而不是:

<item name="android:preferenceStyle">@style/PreferenceThemeOverlay</item>

将此行添加到您的Apptheme

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

关于java - 如何将主题应用到首选项屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41662763/

相关文章:

android - 构建cocos2d-x android项目失败: Unknown EABI object attribute 44

android - 我可以将数据从 firebase 发送到我的网站吗?

android - Google Play Developer Console - 无法更新应用程序,说应用程序标题已被包名称使用...我自己的包名称

android - 更改 Searchview 焦点颜色

java - 在 Android 中更改 Button 和 TextView 的选择颜色

java - 添加新文件后如何刷新eclipse中的工作区?

java - Spring 集成结束流程

react-native - 如何在样式表创建中使用 useTheme 钩子(Hook)?

java - 扩展 AsyncTask 而不覆盖其参数

java - 从 MainActivity 通知 onPostExecute 的 fragment