实现效果图

实现原理

外面是一个RadioGroup,根据要显示的数据模型,用addView()方法把RadioButton动态添加到RadioGroup里面。

注意RadioGroup的直接子控件是RadioButton

示例代码

  • RadioGroup

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    <RadioGroup
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:orientation="vertical"
    android:divider="@drawable/shape_divider_neworder_radiogroup"
    android:showDividers="end|middle"
    android:dividerPadding="8dp"/>
    ```
    * shape_divider_neworder_radiogroup
    ```html
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#f2f2f2" />
    <stroke android:width="0dp" android:color="@android:color/transparent" />
    <size android:width="100dp" android:height="1dp" />
    <padding android:right="0dp"
    android:left="10dp"/>
    </shape>
  • RadioButton

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    <RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:button="@null"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:textColor="#8c8c8c"
    android:textSize="12sp"
    android:layout_marginTop="4dp"
    android:layout_marginBottom="4dp"
    />
    ```
    * 添加控件代码
    ```java
    Drawable drawableLeft,drawableRight;
    drawableLeft = new BitmapDrawable(bitmap);
    drawableLeft.setBounds(0,
    0,
    drawableLeft.getMinimumWidth(),
    drawableLeft.getMinimumHeight()
    );
    drawableRight =getResources()
    .getDrawable(R.drawable.selector_neworder_radiobut);
    drawableRight.setBounds(0,
    0,
    drawableRight.getMinimumWidth(),
    drawableRight.getMinimumHeight()
    );
    radiobutton.setCompoundDrawables(drawableLeft
    ,null
    ,drawableRight
    ,null);
    radioGroupTransportCompany.addView(radiobutton);
<<<<<<< HEAD:2016/06/26/Android-RadioButton实践/index.html