RadioButton设置图片大小

    前几天写demo,底部是一个RadioGroup+RadioButton的导航按钮,文字在下面,图片在上面。随便导入了几张图片,本来图片很大,想着xml里面能不能设置,以前项目中UI给的图刚好合适,也就没有注意,结果发现xml里面无法设置图片的大小,文字直接被挤掉了。很郁闷啊,搞了半天,查源码,百度,最后发现,xml里面根本不能修改图片的大小。倒是总结了两种可行的方法,供大家参考。

第一种:在代码中修改。
第一步:设置布局,找到RadioButton控件
setContentView(R.layout.activity_main);
RadioButton  rb=(RadioButton)findViewById(R.id.rb);
第二步:找到你要设置大小的图片资源
Drawable drawable = getResources().getDrawable(R.drawable.img_rb);//找到图片
drawable.setBounds(0, 0, 60, 60);//最关键的一步,给图片设置大小,4个参数分别是左上右下,我看好多人对这个左上右下不理解,看下源码
rb.setCompoundDrawables(null, drawable, null, null);

++++++++++++++++++++++源码++++++++++++++++++++++++++++++++++++++++++++++++++
/**
* Specify a bounding rectangle for the Drawable. This is where the drawable
* will draw when its draw() method is called.
* 这句话的意思是说:指定一个可拉边界的矩形。
* ok,就是说给图片一个矩形,起始大小都为0,我们分别指定图片右边、图片下边距离矩形左边和上边的大小,这不就是图片的大小了嘛
*/
public void setBounds(int left, int top, int right, int bottom) {
    Rect oldBounds = mBounds;
    if (oldBounds == ZERO_BOUNDS_RECT) {
        oldBounds = mBounds = new Rect();
    }
    if (oldBounds.left != left || oldBounds.top != top ||
            oldBounds.right != right || oldBounds.bottom != bottom) {
        if (!oldBounds.isEmpty()) {
            // first invalidate the previous bounds
            invalidateSelf();
        }
        mBounds.set(left, top, right, bottom);
        onBoundsChange(mBounds);
    }
}
++++++++++++++++++++++++源码++++++++++++++++++++++++++++++++++++++++++++++++

第二种:自定义一个MyRadioButton。原理跟第一种是一样的,直接上代码吧。
第一步:在attrs文件夹下面定义一个图片引用的属性
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyRadioButton">
        <attr name="drawableTop" format="reference" />
    </declare-styleable>
</resources>

第二步:定义一个MyRadioButton类,继承原生的RadioButton
public class MyRadioButton extends RadioButton {
    private Drawable drawable;
    public MyRadioButton(Context context) {
        super(context);
    }

    public MyRadioButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyRadioButton);//获取我们定义的属性
        drawable = typedArray.getDrawable(R.styleable.MyRadioButton_drawableTop);
        drawable.setBounds(0, 0, 60, 60);
        setCompoundDrawables(null, drawable, null, null);
    }
}
第三步:xml中使用它:
<com.myapp.view.MyRadioButton
     android:id="@+id/rb1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_gravity="center"
     android:layout_weight="1"
     android:button="@null"
     android:gravity="center"
     android:text="哈哈1"
     attr:drawableTop="@drawable/单个图片或者选择器图片" />
<com.myapp.view.MyRadioButton
     android:id="@+id/rb2"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_gravity="center"
     android:layout_weight="1"
     android:button="@null"
     android:gravity="center"
     android:text="哈哈2"
     attr:drawableTop="@drawable/-----" />
<com.myapp.view.MyRadioButton
     android:id="@+id/rb3"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_gravity="center"
     android:layout_weight="1"
     android:button="@null"
     android:gravity="center"
     android:text="哈哈3"
     attr:drawableTop="@drawable/----" />
运行:完美!

需要主意的是:要是在RadioGroup中默认选中第几个,但是点击后不互斥,这是因为没有给RadioButton写id。

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值