1. 无法识别attrs.xml文件里自己定义的属性
在attrs.xml文件下已经定义了属性,如代码片段2,但是尝试用代码片段1
//代码片段1
TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.MRadio,0,0);
String mTitle = typedArray.getString(R.styleable.radio_title);
来获取对应的属性的时候却被标红,此时应该用typedArray.getString(R.styleable.MRadio_radio_title)来获取对应属性。
//代码片段2
<declare-styleable name="MRadio">
<attr name="radio_title" format="string" />
<attr name="radio_sel_pic" format="reference" />
</declare-styleable>
2.获取attrs.xml文件里的定义为reference类型的属性
如获取drawable资源 :
Drawable mSelPic = typedArray.getDrawable(R.styleable.MRadio_radio_sel_pic);
本文详细解析了在Android开发中如何正确读取attrs.xml文件中自定义的属性,包括字符串类型和引用类型属性的获取方式,强调了属性名前缀在调用中的重要性。
299

被折叠的 条评论
为什么被折叠?



