我有PreferenceFragment
EditTextPreference
,用户可以在其中指定textSize
。我想在当前textSize
中显示此首选项的摘要。在偏好改变后,我设法更改了摘要的textSize
:
public class PrefsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
@Override
public void onResume() {
super.onResume();
getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onPause() {
getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
super.onPause();
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
float size = Float.parseFloat(sharedPreferences.getString("size","-1"));
if(size>0) {
LinearLayout linearLayout = (LinearLayout) getView();
ListView listView = (ListView) linearLayout.getChildAt(0);
LinearLayout linearLayout1 = (LinearLayout) listView.getChildAt(1);
RelativeLayout relativeLayout = (RelativeLayout) linearLayout1.getChildAt(1);
TextView summary = (TextView) relativeLayout.getChildAt(1);
summary.setTextSize(size);
}
}
}
但是在显示PreferenceFragment
屏幕时我无法设置它。我在addPreferencesFromResource
和onResume
之后(super
之后)尝试过,但两次ListView
仍为空。
有人知道它何时被填满?