/*** 改变单选按钮左上右下部分的图片大小
*@authorAdministrator*/
public class MyRadioButton extendsRadioButton {private int mDrawableSize;//xml文件中设置的大小
publicMyRadioButton(Context context) {this(context, null, 0);
}publicMyRadioButton(Context context, AttributeSet attrs) {this(context, attrs, 0);
}public MyRadioButton(Context context, AttributeSet attrs, intdefStyle) {super(context, attrs, defStyle);
Drawable drawableLeft= null, drawableTop = null, drawableRight = null, drawableBottom = null;
TypedArray a=context.obtainStyledAttributes(attrs, R.styleable.MyRadioButton);//解析自定义的属性,为图片设置尺寸大小
int n =a.getIndexCount();for (int i = 0; i < n; i++) {int attr =a.getIndex(i);switch(attr) {caseR.styleable.MyRadioButton_drawableSize:
mDrawableSize= a.getDimensionPixelSize(R.styleable.MyRadioButton_drawableSize, 50);break;caseR.styleable.MyRadioButton_drawableTop:
drawableTop=a.getDrawable(attr);break;caseR.styleable.MyRadioButton_drawableBottom:
drawableRight=a.getDrawable(attr);break;caseR.styleable.MyRadioButton_drawableRight:
drawableBottom=a.getDrawable(attr);break;caseR.styleable.MyRadioButton_drawableLeft:
drawableLeft=a.getDrawable(attr);break;
}
}
a.recycle();//回收
setCompoundDrawablesWithIntrinsicBounds(drawableLeft, drawableTop, drawableRight, drawableBottom);
}/*** 为图片设置边界,即要显示的大小*/
public voidsetCompoundDrawablesWithIntrinsicBounds(Drawable left,
Drawable top, Drawable right, Drawable bottom) {if (left != null) {
left.setBounds(0, 0, mDrawableSize, mDrawableSize);//为图片设置边界
}if (right != null) {
right.setBounds(0, 0, mDrawableSize, mDrawableSize);
}if (top != null) {
top.setBounds(0, 0, mDrawableSize, mDrawableSize);
}if (bottom != null) {
bottom.setBounds(0, 0, mDrawableSize, mDrawableSize);
}//设置合成的图片文字
setCompoundDrawables(left, top, right, bottom);
}
}