【问题标题】:Missing switch for SwitchPreferenceCompatSwitchPreferenceCompat 缺少开关
【发布时间】:2016-05-17 15:22:24
【问题描述】:

我创建了一个 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>

但是,由于某种原因,我找不到,没有显示开关。我没有收到任何错误。看起来是这样的:


代码

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>

【问题讨论】:

  • defaultValue 属性应该采用布尔值而不是字符串
  • 没关系。提供的资源是布尔值
  • 你能贴出你的 PreferenceFragment 的代码吗
  • addPreferencesFromResource(R.xml.preferences);移动到片段的onCreate。我总是把我的放在那里
  • 除此之外,我认为一切都很好

标签: android preferencefragment


【解决方案1】:

创建我自己的 PreferenceTheme 似乎解决了我的问题,这与您看到的问题相似。

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

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

【讨论】:

    猜你喜欢
    • 2018-09-25
    • 2015-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-15
    • 2020-10-29
    • 2017-03-02
    相关资源
    最近更新 更多