Android:PreferenceScreen 的自定义 Xml 属性

发布于 2024-09-25 15:08:01 字数 688 浏览 1 评论 0原文

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

I use custom xml attributes for preferences. The preferences are inflated from xml.

I managed to create and read custom xml attributes for EditTextPreference, ListPreference and CheckBoxPreference by creating custom classes which inherit from the respective preference class.

In the constructor of the class I can read the attributes like so:

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);
    }
}

My problem is that I can't do this for the PreferenceScreen class, as it is a final class. So my question is: Is there any way I can read the custom attributes of a PreferenceScreen?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜吻♂芭芘 2024-10-02 15:08:01

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

Probably not by the same techniques that you are using. Remember, though, that preference XML files are just XML resources. You can get a parser for the file via getResources().getXml() from your PreferenceActivity. From there, you can read whatever you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文