Android:自定义MyRadioButton

1、attrs.xml

<declare-styleable name="MyRadioButton">
        <attr name="rb_left_width" format="dimension" />
        <attr name="rb_left_height" format="dimension" />

        <attr name="rb_top_width" format="dimension" />
        <attr name="rb_top_height" format="dimension" />
        <attr name="rb_right_width" format="dimension" />
        <attr name="rb_right_height" format="dimension" />
        <attr name="rb_bottom_width" format="dimension" />
        <attr name="rb_bottom_height" format="dimension" />
    </declare-styleable>

2、MyRadioButton

@SuppressLint("AppCompatCustomView")
public class MyRadioButton extends AppCompatRadioButton {
    private float mLeftWidth;
    private float mLeftHeight;
    private float mTopWidth;
    private float mTopHeight;
    private float mRightWidth;
    private float mRightHeight;
    private float mBottomWidth;
    private float mBottomHeight;

    public MyRadioButton(Context context) {
        super(context);
    }

    public MyRadioButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray t = context.obtainStyledAttributes(attrs, R.styleable.MyRadioButton);
        mLeftWidth = t.getDimension(R.styleable.MyRadioButton_rb_left_width, dip2px(context, 25));
        mLeftHeight = t.getDimension(R.styleable.MyRadioButton_rb_left_height, dip2px(context, 25));
        mTopWidth = t.getDimension(R.styleable.MyRadioButton_rb_top_width, dip2px(context, 25));
        mTopHeight = t.getDimension(R.styleable.MyRadioButton_rb_top_height, dip2px(context, 25));
        mRightWidth = t.getDimension(R.styleable.MyRadioButton_rb_right_width, dip2px(context, 25));
        mRightHeight = t.getDimension(R.styleable.MyRadioButton_rb_right_height, dip2px(context, 25));
        mBottomWidth = t.getDimension(R.styleable.MyRadioButton_rb_bottom_width, dip2px(context, 25));
        mBottomHeight = t.getDimension(R.styleable.MyRadioButton_rb_bottom_height, dip2px(context, 25));
        t.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //让RadioButton的图标可调大小 属性:
        Drawable drawableLeft = this.getCompoundDrawables()[0];//获得文字左侧图片
        Drawable drawableTop = this.getCompoundDrawables()[1];//获得文字顶部图片
        Drawable drawableRight = this.getCompoundDrawables()[2];//获得文字右侧图片
        Drawable drawableBottom = this.getCompoundDrawables()[3];//获得文字底部图片
        if (drawableLeft != null) {
            drawableLeft.setBounds(0, 0, (int) mLeftWidth, (int) mLeftHeight);
        }
        if (drawableTop != null) {
            drawableTop.setBounds(0, 0, (int) mTopWidth, (int) mTopHeight);
        }
        if (drawableRight != null) {
            drawableRight.setBounds(0, 0, (int) mRightWidth, (int) mRightHeight);
        }
        if (drawableBottom != null) {
            drawableBottom.setBounds(0, 0, (int) mBottomWidth, (int) mBottomHeight);
        }
        this.setCompoundDrawables(drawableLeft, drawableTop, drawableRight, drawableBottom);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }

    public static int dip2px(Context context, float dpValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);

    }

}

3、使用

<RadioGroup
                    android:id="@+id/rg_change_fragment"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <view.MyRadioButton
                        android:id="@+id/rb_1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:button="@null"
                        android:checked="true"
                        android:drawableLeft="@drawable/selector_item_selected"
                        android:drawablePadding="10dp"
                        android:gravity="center"
                        android:text="未审核"
                        android:textColor="@color/color_333"
                        android:textSize="14sp"
                        app:rb_left_height="15dp"
                        app:rb_left_width="15dp" />

                    <view.MyRadioButton
                        android:id="@+id/rb_2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="20dp"
                        android:button="@null"
                        android:checked="false"
                        android:drawableLeft="@drawable/selector_item_selected"
                        android:drawablePadding="10dp"
                        android:gravity="center"
                        android:text="已审核"
                        android:textColor="@color/color_333"
                        android:textSize="14sp"
                        app:rb_left_height="15dp"
                        app:rb_left_width="15dp" />
                </RadioGroup>
mRgChangeFragment.setOnCheckedChangeListener((radioGroup, i) -> {
            switch (i) {
                case R.id.rb_1:
                   
                   
                    break;
                case R.id.rb_2:
                    

                    break;
            }
        });

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值