标签:
在使用settings的时候,如果通过代码的形式添加CheckboxPreference,使用setDependency()方法的注意事项:
private static final String PARENT_CHECKBOX_PREFERENCE = "parent_checkbox_preference"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this); setPreferenceScreen(root); // 这里必须先添加 populatePreferenceHierarchy(root); // 然后再在root树上构建preference,代码执行顺序不能颠倒 private void populatePreferenceHierarchy(PreferenceScreen pScreenRoot) { // Preference attributes PreferenceCategory prefAttrsCat = new PreferenceCategory(this); prefAttrsCat.setTitle("preference_attributes"); pScreenRoot.addPreference(prefAttrsCat); // Visual parent toggle preference CheckBoxPreference parentCheckBoxPref = new CheckBoxPreference(this); parentCheckBoxPref.setKey(PARENT_CHECKBOX_PREFERENCE); parentCheckBoxPref.setTitle("title_parent_preference"); parentCheckBoxPref.setSummary("summary_parent_preference"); prefAttrsCat.addPreference(parentCheckBoxPref); // Visual child toggle preference // See res/values/attrs.xml for the <declare-styleable> that defines TogglePrefAttrs. TypedArray typeA = obtainStyledAttributes(R.styleable.TogglePrefAttrs); CheckBoxPreference childCheckBoxPref = new CheckBoxPreference(this); childCheckBoxPref.setKey("child_checkbox_preference"); childCheckBoxPref.setTitle("title_child_preference"); childCheckBoxPref.setSummary("summary_child_preference"); childCheckBoxPref.setLayoutResource(typeA.getResourceId(R.styleable.TogglePrefAttrs_android_preferenceLayoutChild, 0)); prefAttrsCat.addPreference(childCheckBoxPref); // 这里必须先添加 childCheckBoxPref.setDependency(PARENT_CHECKBOX_PREFERENCE); // 然后再设置依赖 typeA.recycle(); }
Dependency "XXXX" not found for preference "XXXX"
标签:
原文地址:http://my.oschina.net/artshell/blog/396851