动态设置Button大小及点击事件:
在布局中写下这段代码:新创建一个class 名字叫MyRadioButton 继承RdioButton
package duan.com.homework.helper; import android.content.Context; import android.content.res.TypedArray; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.widget.RadioButton; import duan.com.homework.R; /** * Created by Administrator on 2016/9/22. */ public class MyRadioButton extends RadioButton { public MyRadioButton(Context context) { super(context); } public MyRadioButton(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyRadioButton); //drawableSize = a.getDimensionPixelSize(R.styleable.MyRadioButton_rbDrawableTopSize, 50); Drawable drawableTop = a.getDrawable(R.styleable.MyRadioButton_rbDrawableTop); a.recycle(); setCompoundDrawablesWithIntrinsicBounds(null,drawableTop,null,null); } public MyRadioButton(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public void setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom) { super.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom); if (top!=null){ top.setBounds(0,0,50,50); } setCompoundDrawables(left,top,right,bottom); } }然后在res下创建attrs:
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="MyRadioButton"> <attr name="rbDrawableTopSize" format="dimension"/> <attr name="rbDrawableTop" format="reference"