radiobutton设置drawableTop图片的大小

在开发应用时,很多时候会遇到页面与按钮的联动,当使用RadioButton与ViewPager实现联动时,RadioButton会设置图片和文字,但是由于RadioButton设置的图片无法自定义的修改图片大小,导致界面的美观性差,因此需要重新设置RadioButton的图片大小。

多余的不说了直接上代码了!!!

 	mRbRecommend = (RadioButton) findViewById(R.id.rb_recommend);
        mRbMore = (RadioButton) findViewById(R.id.rb_more);
        mRbSetting = (RadioButton) findViewById(R.id.rb_setting);
        mRbMe = (RadioButton) findViewById(R.id.rb_me);
        RadioButton[] rbs = new RadioButton[4];
        rbs[0] =mRbRecommend;
        rbs[1] = mRbMore;
        rbs[2] = mRbSetting;
        rbs[3] = mRbMe;
        for (RadioButton rb : rbs) {
            //挨着给每个RadioButton加入drawable限制边距以控制显示大小
            Drawable[] drawables = rb.getCompoundDrawables();
            //获取drawables
            Rect r = new Rect(0, 0, drawables[1].getMinimumWidth()*2/3, drawables[1].getMinimumHeight()*2/3);
            //定义一个Rect边界
            drawables[1].setBounds(r);
            //给指定的radiobutton设置drawable边界
//            if (rb.getId() == R.id.rb_more) {
//                r = new Rect(0, 0, drawables[1].getMinimumWidth(), drawables[1].getMinimumHeight());
//                drawables[1].setBounds(r);
//            }
            //添加限制给控件
            rb.setCompoundDrawables(null,drawables[1],null,null);
        }
更改前的图片:

更改后的图片:

猜你喜欢

转载自blog.csdn.net/qq_36071384/article/details/78475579