3

我使用自定义 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 的自定义属性?

4

1 回答 1

1

可能与您使用的技术不同。但请记住,首选项 XML 文件只是 XML 资源。您可以通过getResources().getXml()PreferenceActivity. 从那里,您可以阅读任何您想要的内容。

于 2010-09-26T19:32:24.970 回答