【问题标题】:How to apply theme to preferencescreen如何将主题应用于偏好屏幕
【发布时间】:2017-01-15 15:21:38
【问题描述】:

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

以下是清单代码

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

你能建议一下吗,我是第一次使用主题并且对此感到困惑。仍然出现同样的错误。

【问题讨论】:

    标签: java android themes preferencefragment


    【解决方案1】:

    manifest 的活动标签中试试这个:

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

    您正在加载preference fragmentactivity 没有设置主题。

    也在你的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.lang.RuntimeException:无法启动活动 ComponentInfo{jss.fragments/jss.fragments.MainActivity}:java.lang.IllegalStateException:此活动已经有一个由窗户装饰。不要在主题中请求 Window.FEATURE_SUPPORT_ACTION_BAR 并将 windowActionBar 设置为 false 以使用工具栏。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多