我有偏好xml ..
首先从XML资源加载首选项
addPreferencesFromResource(R.xml.preferences);
并为其设置一些默认值...设置默认值后,我需要隐藏(不删除)我的首选屏幕
我的偏好XML是
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<Preference
android:title="Account Settings"
android:key="customPref" />
<PreferenceScreen android:title="@string/account_1"
android:key="account">
<EditTextPreference
android:key="username"
android:title="@string/settings_username"
android:singleLine="true" />
<EditTextPreference
android:key="password"
android:title="@string/settings_password"
android:password="true"
android:singleLine="true" />
</PreferenceScreen>
<PreferenceScreen android:title="@string/account_2"
android:key="account1">
<EditTextPreference
android:key="username1"
android:title="@string/settings_username"
android:singleLine="true" />
<EditTextPreference
android:key="password1"
android:title="@string/settings_password"
android:password="true"
android:singleLine="true" />
</PreferenceScreen>
</PreferenceScreen>
我需要隐藏PreferenceScreen
title ="@string/account_2
答案 0 :(得分:3)
简单如下:
Preference preference = (Preference) findPreference("pref");
PreferenceScreen preferenceScreen = (PreferenceScreen) findPreference("pref_screen");
PreferenceCategory preferenceCategory = (PreferenceCategory) findPreference("pref_category");
隐藏偏好:
PreferenceGroup preferenceParent = getParent(preference);
preferenceParent.removePreference(preference);
隐藏首选项屏幕:
PreferenceGroup preferenceScreenParent = getParent(preferenceScreen);
preferenceScreenParent.removePreference(preferenceScreen);
隐藏PreferenceCategory:
PreferenceGroup preferenceCategoryParent = getParent(preferenceCategory);
preferenceCategoryParent.removePreference(preferenceCategory);
同样的方式是EditTextPreference,CheckBoxPreference,.....
答案 1 :(得分:2)
您可以复制XML。然后删除第二部分,如下所示:
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<Preference
android:title="Account Settings"
android:key="customPref" />
<PreferenceScreen android:title="@string/account_1"
android:key="account">
<EditTextPreference
android:key="username"
android:title="@string/settings_username"
android:singleLine="true" />
<EditTextPreference
android:key="password"
android:title="@string/settings_password"
android:password="true"
android:singleLine="true" />
</PreferenceScreen>
</PreferenceScreen>
然后检查您的偏好,看它是否已经初始化。
如果是,则加载上面的XML,否则加载第一个。
答案 2 :(得分:0)
如果您同时引用了Preference及其父级(PreferenceCategory或PreferenceScreen)
myPreferenceScreen.remove(myPreference);