我用它来设置pref
PreferenceFragment fragment;
try {
getClass().getMethod("getFragmentManager");
AddResourceApi11AndGreater();
} catch (NoSuchMethodException e) { //Api < 11
AddResourceApiLessThan11();
}
@SuppressWarnings("deprecation")
protected void AddResourceApiLessThan11()
{
addPreferencesFromResource(prefs);
}
@TargetApi(11)
protected void AddResourceApi11AndGreater()
{
PreferenceFragment fr = new PF();
fragment = fr;
getFragmentManager().beginTransaction().replace(android.R.id.content, fr).commit();
}
@TargetApi(11)
public static class PF extends PreferenceFragment
{
@Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(AyarlarScreen.prefs);
}
}
我仍然从findPreference和fragment.findPreference
中获取null(我是4.0.4 android。所以它使用Api11andGreter。所以我保存片段ref,然后用它来获取pref。如果我手动使用api11andlower,我可以得到偏好)