Android:PreferenceScreen的自定义Xml属性

时间:2010-09-26 19:15:51

标签: android android-preferences

我对首选项使用自定义xml属性。首选项从xml中膨胀。

我设法通过创建从相应的首选项类继承的自定义类来为EditTextPreference,ListPreference和CheckBoxPreference创建和读取自定义xml属性。

在类的构造函数中,我可以读取如下属性:

public class CustomTextPreference extends EditTextPreference {
    public CustomTextPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.PreferenceCustomAttrs);
        x = a.getString(R.styleable.PreferenceCustomAttrs_x);
        y = a.getString(R.styleable.PreferenceCustomAttrs_y);
    }
}

我的问题是我不能为PreferenceScreen类做这个,因为它是最后一个类。所以我的问题是:有什么办法可以读取PreferenceScreen的自定义属性吗?

1 个答案:

答案 0 :(得分:1)

可能不是您使用的技术相同。但请记住,首选XML文件只是XML资源。您可以通过getResources().getXml()的{​​{1}}获取该文件的解析器。从那里,你可以阅读你想要的任何东西。