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