android 修改RadioButton的drawTop图片大小

最近做项目用的是RadioGroup+Fragment进行来回切换,这样方便也很多,但是也出现问题,drabTop的图片不能改变大小,所以写此博客记录一下

效果图


/**
 * Created by Sinocall on 2015-10-21.
 */
public class MyRadioButton extends RadioButton{
    //图片大小
    //private int drawableSize;

    public MyRadioButton(Context context) {
        this(context,null);
    }

    public MyRadioButton(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyRadioButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

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

    @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,70,60);
        }
        setCompoundDrawables(left,top,right,bottom);
    }
}
在values下新建一个attrs.xml然后把下面代码复制进去就好
<declare-styleable name="MyRadioButton">
    <attr name="rbDrawableTopSize" format="dimension"/>
    <attr name="rbDrawableTop" format="reference"/>
</declare-styleable>
在布局文件中直接使用,注意声明命名空间。就这么简单任性即可解决,本人也是在网上搜集之后改的!

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值