1.在@H_403_2@values文件中,新建attrs.xml@H_403_2@文件(如果原来没有),内容大致是这样,@H_403_2@
<?xmlversion="1.0"encoding="utf-8"?>
<resources>
<!--自定义圆角@H_403_2@imageview@H_403_2@属性@H_403_2@-->@H_403_2@
<declare-styleablename="RoundImageView">
<attrname="xhradius"format="dimension"></attr>
<attrname="xhtype">
<enumname="fillet"value="0"></enum>
<enumname="round"value="1"></enum>
</attr>
</declare-styleable>
</resources>
解读一下,<declare-styleablename="RoundImageView">申明一个控件
,其中name="RoundImageView"是定义这个控件的那个java@H_403_2@类的名称@H_403_2@.@H_403_2@
<attrname="xhradius"format="dimension"></attr>就是自定义的一条属性,申明了属性的名称和属性的类型。
2.在自定义View@H_403_2@的@H_403_2@java@H_403_2@类中,使用几个构造方法来接收自定义的值。如@H_403_2@
//半径@H_403_2@
privatefloatradius=0;
//类型@H_403_2@
privateinttype;
publicRoundImageView(Contextcontext,AttributeSetattrs){
this(context,attrs,0);//没有这个方法就会出错。没有这句话就会读不到值@H_403_2@
}
publicRoundImageView(Contextcontext,AttributeSetattrs,intdefStyleAttr){
super(context,defStyleAttr);
TypedArraytypedArray=context.getTheme().obtainStyledAttributes(attrs,R.styleable.RoundImageView,defStyleAttr,0);
radius=typedArray.getDimension(R.styleable.RoundImageView_xhradius,0);//默认半径@H_403_2@0@H_403_2@
type=typedArray.getInt(R.styleable.RoundImageView_xhtype,0);//默认圆角类型@H_403_2@
typedArray.recycle();
paint=newPaint();
}