RadioButton自定义图标跟选中文本状态

RadioButton平时用的比较少,突然用到反而遇到点小问题,为了避免以后又忘记了浪费时间,还是稍微记录一下。

实现效果如图,修改选中时候的文本跟小图标:

代码很简单,直接上代码。再稍微备注一下需要注意的地方。

  <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <RadioButton
            android:padding="5dp"
            android:button="@drawable/radiobutton_shape"
            android:text="radioButton1"
            android:textColor="@drawable/color_radiobutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
             />

        <RadioButton
            android:padding="5dp"
            android:button="@drawable/radiobutton_shape"
            android:textColor="@drawable/color_radiobutton"
            android:text="radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RadioGroup>

1.修改文本颜色,直接用”textColor“属性,然后给它一个选择器。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:color="#0000ff"/>
    <!-- not selected -->
    <item android:color="#000000"/>
</selector>

2.修改图标,直接用”button“属性,然后给它一个选择器

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/node_propertyset_checkbox_selected" android:state_checked="true" android:state_enabled="true"></item>
    <item android:drawable="@drawable/node_propertyset_checkbox_default" android:state_checked="false" android:state_enabled="true"></item>
</selector>
最后备注一下需要注意点的地方,这里设置图标之后,是会被压缩的(头部跟尾部被削去了一点),我也不知道为何,同样的图标放在Checkbox是正常的,应该不是切图的问题,然后这里只能通过padding属性来设置,或者直接给RadioButton写死固定高度。





打个广告,本人开始做微信公众号运营,愿意支持的就扫码关注一下,O(∩_∩)O谢谢,主要做电影解说这一块,每个程序员都想跳出写代码的坑,希望多多支持



猜你喜欢

转载自blog.csdn.net/u014369799/article/details/50600344