在PreferenceFregment中构造界面,简单省事的方法就是使用findPreference然后在xml里把UI写好。在代码中动态的添加UI内容也是需要的。核心代码是:
PreferenceScreen a = getPreferenceScreen();
Preference pfs = new Preference(getActivity());
pfs.setTitle("aaa");
pfs.setSummary("connected");
a.addItemFromInflater(pfs);
放在代码环境中就是:
public void onCreate(Bundle icicle) {
// TODO Auto-generated method stub
super.onCreate(icicle);
Log.i(TAG, "--------onCreate");
addPreferencesFromResource(R.xml.test);
//PreferenceScreen a = (PreferenceScreen)getPreferenceManager().findPreference("other");
PreferenceScreen a = getPreferenceScreen();
Preference pfs = new Preference(getActivity());
pfs.setTitle("aaa");
pfs.setSummary("connected");
a.addItemFromInflater(pfs);
}