【问题标题】:Android: Custom Xml Attributes for PreferenceScreenAndroid:PreferenceScreen 的自定义 Xml 属性
【发布时间】:2010-09-26 19:15:51
【问题描述】:

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

【问题讨论】:

    标签: android android-preferences


    【解决方案1】:

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

    【讨论】:

    • 感谢您的回答!这将是一个选项,但是使用这种方法,自定义属性不会成为属性类的成员。我将不得不维护从属性到其自定义属性的手动映射(例如,通过字典),无论如何我已经这样做了,并且希望摆脱自定义属性。
    猜你喜欢
    • 1970-01-01
    • 2014-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-25
    • 1970-01-01
    • 2016-01-27
    • 1970-01-01
    相关资源
    最近更新 更多