我遵循了this可能的解决方案,但这仅适用于root PreferenceScreen。我正在使用v7支持PreferenceScreen和PreferenceFragmentCompat。
preference.xml
<android.support.v7.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v7.preference.PreferenceScreen
android:key="pref_key_r"
android:persistent="false"
android:summary="@string/a"
android:title="@string/pref_title_r">
</android.support.v7.preference.PreferenceScreen>
</android.support.v7.preference.PreferenceScreen>
SettingsFragment.java
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
addPreferencesFromResource(R.xml.preferences);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PreferenceScreen preferenceScreen = (PreferenceScreen) getPreferenceScreen().findPreference("pref_key_r");
Preference preference = new Preference(preferenceScreen.getContext());
preference.setKey("pref_key_sp");
preference.setTitle(R.string.sp);
preferenceScreen.addPreference(preference);
}
我还缺少什么?