缺少SwitchPreferenceCompat的开关

时间:2016-05-17 15:22:24

标签: android preferencefragment

我创建了一个PreferenceScreen,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <android.support.v7.preference.PreferenceCategory
        android:title="@string/title_telephone">

        <android.support.v7.preference.SwitchPreferenceCompat
            android:key="checkbox_preference_telephone_recording"
            android:title="@string/checkbox_telephone_recording"
            android:defaultValue="@string/default_checkbox_telephone_recording"/>

        <android.support.v7.preference.SwitchPreferenceCompat
            android:key="checkbox_preference_telephone_accel"
            android:title="@string/checkbox_telephone_accel"
            android:defaultValue="@string/default_checkbox_telephone_accel"/>

    </android.support.v7.preference.PreferenceCategory>
</android.support.v7.preference.PreferenceScreen>

然而,由于某些原因我找不到,开关不会显示。我没有得到任何错误。这就是它的样子:

enter image description here

代码

public class SettingsFragment extends PreferenceFragmentCompat {

    private Preference mListPreference;


    @Override
    public void onCreatePreferences(Bundle bundle, String s) {
        addPreferencesFromResource(R.xml.preferences);
    }
}

styles.xml

<resources>>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
        <item name="preferenceTheme">@style/PreferenceThemeOverlay.v14.Material</item>
        <item name="preferenceStyle">@style/PreferenceThemeOverlay.v14.Material</item>
    </style>

    <style name="MyActionBarTheme" parent="android:Widget.Material.Light.ActionBar">
        <item name="android:background">@color/colorAccent</item>
        <item name="android:titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
    </style>

    <style name="MyTheme.ActionBar.TitleTextStyle" parent="@android:style/TextAppearance.Material.Widget.ActionBar.Title">
        <item name="android:textColor">@color/colorPrimary</item>
    </style>

    <style name="AppTheme" parent="android:Theme.Material.Light">
        <!-- 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:colorBackground">@color/background_material_light</item>
        <item name="android:colorPrimary">@color/primary_material_light</item>
        <item name="android:actionBarStyle">@style/MyActionBarTheme</item>
        <item name="preferenceTheme">@style/PreferenceThemeOverlay.v14.Material</item>
        <item name="preferenceStyle">@style/PreferenceThemeOverlay.v14.Material</item>
    </style>
</resources>

1 个答案:

答案 0 :(得分:0)

创建我自己的偏好主题似乎解决了我的问题,这类似于你所看到的问题。

如果没有这样做,至少它会显示如何创建自己的首选项主题,以便您可以覆盖其他属性:)

<resources>
    <style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
        <item name="preferenceTheme">@style/PreferenceTheme</item>
        <item name="preferenceStyle">@style/PreferenceTheme</item>
        ...
    </style>

    <style name="PreferenceTheme" parent="PreferenceThemeOverlay">
        <item name="android:textColor">#000000</item>
        <item name="android:textColorPrimary">#000000</item>
        <item name="android:textColorSecondary">#000000</item>
    </style>
</resources>
相关问题