问题描述
我有我的自定义 attr.xml
中,我指定的文档申报,设置样式
:
I have my custom attr.xml
document in which I specified declare-styleable
:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="EditTextValidateablePreference">
<attr name="errorMessage" format="reference" />
</declare-styleable>
</resources>
然后在布局我设置:
Then in layout I set:
<com.xx.yy.EditTextValidateablePreference
...
ns:errorMessage="@string/validation_email_mail_server_invalid"
/>
而在 EditTextValidateable preference.class
我得到它:
String validatorErrorMessage = attrs.getAttributeValue(PREFERENCE_NS, "errorMessage");
validatorErrorMessage
具有类似值: @ 2131099700
怎样才能得到它的整数值与使用它:
How can I get its integer value to use it with:
context.getResources().getString(messageId)
谢谢!
推荐答案
有我建议你参考一个优秀的笼统的回答:Declaring一个定制的Android使用的UI元素XML
There's an excellent general answer which I recommend you to refer to: Declaring a custom android UI element using XML
在特别的,你应该使用Context.obtainStyledAttributes(AttributeSet集,INT [] ATTRS)和TypedArray.getString(int指数),而不是的AttributeSet.getAttributeValue(...)的:
In particular, you should use Context.obtainStyledAttributes(AttributeSet set, int[] attrs) and TypedArray.getString(int index) instead of AttributeSet.getAttributeValue(...):
TypedArray ta = activityContext.obtainStyledAttributes(attrs, R.styleable.EditTextValidateablePreference);
String theString = ta.getString(R.styleable.EditTextValidateablePreference_errorMessage);
ta.recycle();
这篇关于如何从ATTR参考格式类型得到一个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!