Android 获取Java代码中自定义属性的值
我在attrs.xml文件中创建了一个属性:Android 获取Java代码中自定义属性的值,android,Android,我在attrs.xml文件中创建了一个属性: <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="Custom"> <attr name="src" format="integer" /> </declare-styleable> </resource> 在我的代码中,我得到如下属性的值:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Custom">
<attr name="src" format="integer" />
</declare-styleable>
</resource>
在我的代码中,我得到如下属性的值:
attrs.getAttributeIntValue(“mynamespace”、“src”、-1)
它起作用了。我从布局xml文件中获取'src'的值。
但我的问题是为什么android不在R类中生成一个值,这样我就不需要在java代码中再次使用字符串“src”?而是使用
public CustomView(final Context context) {
this(context, null);
}
public CustomView(final Context context,
final AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomView(final Context context,
final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);
final TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.Custom, defStyle, 0);
int src = a.getInt(R.styleable.Custom_src, 0);
a.recycle();
}