当前位置:   article > 正文

android自定义的RadioButton_android extends radiobutton

android extends radiobutton

自定义的RadioButton:可以调节选中框与字体的距离,可以设置选中框的位置

效果如下:


一.在strings.xml里加入下列代码

  1. <declare-styleable name="RadioButton_draw">
  2. <attr name="drawableSize" format="dimension" />
  3. <attr name="drawableTop" format="reference" />
  4. <attr name="drawableLeft" format="reference" />
  5. <attr name="drawableRight" format="reference" />
  6. <attr name="drawableBottom" format="reference" />
  7. </declare-styleable>

二、自定义的RadioButton

  1. package com.example.customradiobutton;
  2. import android.content.Context;
  3. import android.content.res.TypedArray;
  4. import android.graphics.drawable.Drawable;
  5. import android.util.AttributeSet;
  6. import android.widget.RadioButton;
  7. /*
  8. * 描述:RadioButton 图片大小
  9. * 文件名:RadioButtonDrawSize.java7
  10. */
  11. public class RadioButtonDrawSize extends RadioButton {
  12. private int mDrawableSize;// xml文件中设置的大小
  13. public RadioButtonDrawSize(Context context) {
  14. this(context, null, 0);
  15. }
  16. public RadioButtonDrawSize(Context context, AttributeSet attrs) {
  17. this(context, attrs, 0);
  18. }
  19. public RadioButtonDrawSize(Context context, AttributeSet attrs, int defStyle) {
  20. super(context, attrs, defStyle);
  21. Drawable drawableLeft = null, drawableTop = null, drawableRight = null, drawableBottom = null;
  22. TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RadioButton_draw);
  23. int n = a.getIndexCount();
  24. for (int i = 0; i < n; i++) {
  25. int attr = a.getIndex(i);
  26. if (attr == R.styleable.RadioButton_draw_drawableSize) {
  27. mDrawableSize = a.getDimensionPixelSize(R.styleable.RadioButton_draw_drawableSize, 50);
  28. } else if (attr == R.styleable.RadioButton_draw_drawableTop) {
  29. drawableTop = a.getDrawable(attr);
  30. } else if (attr == R.styleable.RadioButton_draw_drawableBottom) {
  31. drawableRight = a.getDrawable(attr);
  32. } else if (attr == R.styleable.RadioButton_draw_drawableRight) {
  33. drawableBottom = a.getDrawable(attr);
  34. } else if (attr == R.styleable.RadioButton_draw_drawableLeft) {
  35. drawableLeft = a.getDrawable(attr);
  36. } else {
  37. }
  38. }
  39. a.recycle();
  40. setCompoundDrawablesWithIntrinsicBounds(drawableLeft, drawableTop, drawableRight, drawableBottom);
  41. }
  42. /**
  43. * RadioButton上、下、左、右设置图标
  44. */
  45. public void setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom) {
  46. if (left != null) {
  47. left.setBounds(0, 0, mDrawableSize, mDrawableSize);
  48. }
  49. if (right != null) {
  50. right.setBounds(0, 0, mDrawableSize, mDrawableSize);
  51. }
  52. if (top != null) {
  53. top.setBounds(0, 0, mDrawableSize, mDrawableSize);
  54. }
  55. if (bottom != null) {
  56. bottom.setBounds(0, 0, mDrawableSize, mDrawableSize);
  57. }
  58. setCompoundDrawables(left, top, right, bottom);
  59. }
  60. }

三、在activity_main.xml使用

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. tools:context="com.example.customradiobutton.MainActivity">
  9. <com.example.customradiobutton.RadioButtonDrawSize
  10. android:id="@+id/radioButton"
  11. style="@style/inform_dialog_RadioButtonDrawSize"
  12. android:layout_margin="10dp"
  13. android:clickable="true"
  14. android:checked="true"
  15. android:text="自定义的button1"
  16. app:drawableLeft="@drawable/jubao_check"
  17. app:drawableSize="30dp" />
  18. <com.example.customradiobutton.RadioButtonDrawSize
  19. android:id="@+id/radioButton2"
  20. style="@style/inform_dialog_RadioButtonDrawSize"
  21. android:layout_margin="10dp"
  22. android:clickable="true"
  23. android:text="自定义的button2"
  24. app:drawableLeft="@drawable/jubao_check"
  25. app:drawableSize="30dp" />
  26. <com.example.customradiobutton.RadioButtonDrawSize
  27. android:id="@+id/radioButton3"
  28. style="@style/inform_dialog_RadioButtonDrawSize"
  29. android:layout_margin="10dp"
  30. android:text="自定义的button3"
  31. android:clickable="true"
  32. app:drawableLeft="@drawable/jubao_check"
  33. app:drawableSize="30dp" />
  34. </RadioGroup>


项目地址:http://download.csdn.net/download/zhang106209/10040731


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

闽ICP备14008679号