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