【发布时间】: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