布局文件如下:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RadioGroup
android:id="@+id/rg_leftMenu"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" >
<cn.vko.teacher.base.MyRadioButton
style="@style/leftMenuPrperty"
android:layout_gravity="center"
android:clickable="true"
android:text="@string/MyLesson"
myradio:drawableSize="30dp"
myradio:drawableTop="@drawable/test" />
<cn.vko.teacher.base.MyRadioButton
style="@style/leftMenuPrperty"
android:clickable="true"
android:text="@string/Task"
app:drawableSize="30dp"
myradio:drawableTop="@drawable/iconfont_zuoyeguanli"
tools:ignore="SpUsage" />
<cn.vko.teacher.base.MyRadioButton
style="@style/leftMenuPrperty"
android:clickable="true"
android:text="@string/Contacts"
myradio:drawableSize="30dp"
myradio:drawableTop="@drawable/iconfont_tongxunlu"
tools:ignore="SpUsage" />
</RadioGroup>
</LinearLayout>
drawable的xml:
<item android:drawable="@drawable/asd1" android:state_checked="true" />
<item android:drawable="@drawable/iconfont_kecheng" android:state_checked="false"/>
重写RadioButton代码如下:
public class MyRadioButton extends RadioButton {
private int mDrawableSize;// xml文件中设置的大小
public MyRadioButton( Context context ) {
this(context, null, 0);
}
public MyRadioButton( Context context, AttributeSet attrs ) {
this(context, attrs, 0);
}
public MyRadioButton( Context context, AttributeSet attrs, int defStyle ) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
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 ) {
case R.styleable.MyRadioButton_drawableSize :
mDrawableSize = a.getDimensionPixelSize(R.styleable.MyRadioButton_drawableSize, 50);
Log.i("MyRadioButton", "mDrawableSize:" + mDrawableSize);
break;
case R.styleable.MyRadioButton_drawableTop :
drawableTop = a.getDrawable(attr);
break;
case R.styleable.MyRadioButton_drawableBottom :
drawableRight = a.getDrawable(attr);
break;
case R.styleable.MyRadioButton_drawableRight :
drawableBottom = a.getDrawable(attr);
break;
case R.styleable.MyRadioButton_drawableLeft :
drawableLeft = a.getDrawable(attr);
break;
default:
break;
}
}
a.recycle();
setCompoundDrawablesWithIntrinsicBounds(drawableLeft, drawableTop, drawableRight, drawableBottom);
}
@Override
public void setCompoundDrawablesWithIntrinsicBounds( 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);
}
@Override
protected void onDraw( Canvas canvas ) {
Drawable[] drawables = getCompoundDrawables();
if ( drawables != null ) {
Drawable drawableTop = drawables[1];
if ( drawableTop != null ) {
float textHeight = measureHeight(getText().toString());
int drawablePadding = getCompoundDrawablePadding();
int drawableHeight = drawableTop.getIntrinsicHeight();
float bodyHeight = textHeight + drawableHeight + drawablePadding;
setPadding(0, (int) (getHeight() - bodyHeight), 0, 20);
canvas.translate(0, 0 - (getHeight() - bodyHeight) / 2);
}
}
super.onDraw(canvas);
}
// 获取文本高度
public int measureHeight( @SuppressWarnings( "unused" ) String text ) {
Paint.FontMetrics fontMetrics = getPaint().getFontMetrics();
return (int) (fontMetrics.descent - fontMetrics.ascent + fontMetrics.leading);
}
}