Android:PreferenceScreen 的自定义 Xml 属性
我使用自定义 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能与您使用的技术不同。但请记住,首选项 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 yourPreferenceActivity
. From there, you can read whatever you want.