何时填充了PreferenceFragment的布局?

时间:2016-04-07 16:11:29

标签: android preferencefragment

我有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屏幕时我无法设置它。我在addPreferencesFromResourceonResume之后(super之后)尝试过,但两次ListView仍为空。 有人知道它何时被填满?

0 个答案:

没有答案