改变单选按钮的图片大小-单选框

控制RadioButton的图片大小:

自定义属性值"

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyRadioButton">  
        <!-- 图片的大小 -->
        <attr name="drawableSize" format="dimension"/>  
        
        <!-- 文字左上右下方位的图片 -->
        <attr name="drawableTop" format="reference"/>  
        <attr name="drawableLeft" format="reference"/>  
        <attr name="drawableRight" format="reference"/>  
        <attr name="drawableBottom" format="reference"/>  
          
    </declare-styleable>  
</resources>

<!-- values/attrs.xml -->

 

/**
 * 改变单选按钮左上右下部分的图片大小
 * @author Administrator
 */
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);  
        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);  
                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;
            }  
        }  
        a.recycle(); //回收
        setCompoundDrawablesWithIntrinsicBounds(drawableLeft, drawableTop, drawableRight, drawableBottom);  
    }  
  
    /**
     * 为图片设置边界,即要显示的大小
     */
    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);  
    }  
  
}  

使用如下:  切记设置 android:clickable="true"  否则不能执行点击事件

<com.example.timepigtest.MyRadioButton
            android:id="@+id/radioButton1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:clickable="true"
            android:gravity="center"
            android:text="首页"
            android:textSize="15sp"
            ts:drawableSize="20dp"    设置图片大小
            ts:drawableTop="@drawable/ic_launcher" />

 效果图:

 

posted @ 2016-11-14 09:00  ts-android  阅读(873)  评论(0编辑  收藏  举报