_android 自定义radiobutton">
当前位置:   article > 正文

Android:自定义MyRadioButton_android 自定义radiobutton

android 自定义radiobutton

1、attrs.xml

  1. <declare-styleable name="MyRadioButton">
  2. <attr name="rb_left_width" format="dimension" />
  3. <attr name="rb_left_height" format="dimension" />
  4. <attr name="rb_top_width" format="dimension" />
  5. <attr name="rb_top_height" format="dimension" />
  6. <attr name="rb_right_width" format="dimension" />
  7. <attr name="rb_right_height" format="dimension" />
  8. <attr name="rb_bottom_width" format="dimension" />
  9. <attr name="rb_bottom_height" format="dimension" />
  10. </declare-styleable>

2、MyRadioButton

  1. @SuppressLint("AppCompatCustomView")
  2. public class MyRadioButton extends AppCompatRadioButton {
  3. private float mLeftWidth;
  4. private float mLeftHeight;
  5. private float mTopWidth;
  6. private float mTopHeight;
  7. private float mRightWidth;
  8. private float mRightHeight;
  9. private float mBottomWidth;
  10. private float mBottomHeight;
  11. public MyRadioButton(Context context) {
  12. super(context);
  13. }
  14. public MyRadioButton(Context context, AttributeSet attrs) {
  15. super(context, attrs);
  16. TypedArray t = context.obtainStyledAttributes(attrs, R.styleable.MyRadioButton);
  17. mLeftWidth = t.getDimension(R.styleable.MyRadioButton_rb_left_width, dip2px(context, 25));
  18. mLeftHeight = t.getDimension(R.styleable.MyRadioButton_rb_left_height, dip2px(context, 25));
  19. mTopWidth = t.getDimension(R.styleable.MyRadioButton_rb_top_width, dip2px(context, 25));
  20. mTopHeight = t.getDimension(R.styleable.MyRadioButton_rb_top_height, dip2px(context, 25));
  21. mRightWidth = t.getDimension(R.styleable.MyRadioButton_rb_right_width, dip2px(context, 25));
  22. mRightHeight = t.getDimension(R.styleable.MyRadioButton_rb_right_height, dip2px(context, 25));
  23. mBottomWidth = t.getDimension(R.styleable.MyRadioButton_rb_bottom_width, dip2px(context, 25));
  24. mBottomHeight = t.getDimension(R.styleable.MyRadioButton_rb_bottom_height, dip2px(context, 25));
  25. t.recycle();
  26. }
  27. @Override
  28. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  29. //让RadioButton的图标可调大小 属性:
  30. Drawable drawableLeft = this.getCompoundDrawables()[0];//获得文字左侧图片
  31. Drawable drawableTop = this.getCompoundDrawables()[1];//获得文字顶部图片
  32. Drawable drawableRight = this.getCompoundDrawables()[2];//获得文字右侧图片
  33. Drawable drawableBottom = this.getCompoundDrawables()[3];//获得文字底部图片
  34. if (drawableLeft != null) {
  35. drawableLeft.setBounds(0, 0, (int) mLeftWidth, (int) mLeftHeight);
  36. }
  37. if (drawableTop != null) {
  38. drawableTop.setBounds(0, 0, (int) mTopWidth, (int) mTopHeight);
  39. }
  40. if (drawableRight != null) {
  41. drawableRight.setBounds(0, 0, (int) mRightWidth, (int) mRightHeight);
  42. }
  43. if (drawableBottom != null) {
  44. drawableBottom.setBounds(0, 0, (int) mBottomWidth, (int) mBottomHeight);
  45. }
  46. this.setCompoundDrawables(drawableLeft, drawableTop, drawableRight, drawableBottom);
  47. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  48. }
  49. public static int dip2px(Context context, float dpValue) {
  50. float scale = context.getResources().getDisplayMetrics().density;
  51. return (int) (dpValue * scale + 0.5f);
  52. }
  53. }

3、使用

  1. <RadioGroup
  2. android:id="@+id/rg_change_fragment"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:orientation="horizontal">
  6. <view.MyRadioButton
  7. android:id="@+id/rb_1"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:button="@null"
  11. android:checked="true"
  12. android:drawableLeft="@drawable/selector_item_selected"
  13. android:drawablePadding="10dp"
  14. android:gravity="center"
  15. android:text="未审核"
  16. android:textColor="@color/color_333"
  17. android:textSize="14sp"
  18. app:rb_left_height="15dp"
  19. app:rb_left_width="15dp" />
  20. <view.MyRadioButton
  21. android:id="@+id/rb_2"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:layout_marginLeft="20dp"
  25. android:button="@null"
  26. android:checked="false"
  27. android:drawableLeft="@drawable/selector_item_selected"
  28. android:drawablePadding="10dp"
  29. android:gravity="center"
  30. android:text="已审核"
  31. android:textColor="@color/color_333"
  32. android:textSize="14sp"
  33. app:rb_left_height="15dp"
  34. app:rb_left_width="15dp" />
  35. </RadioGroup>
  1. mRgChangeFragment.setOnCheckedChangeListener((radioGroup, i) -> {
  2. switch (i) {
  3. case R.id.rb_1:
  4. break;
  5. case R.id.rb_2:
  6. break;
  7. }
  8. });

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小桥流水78/article/detail/985739
推荐阅读
相关标签