RadioGroup+RadioButton进行美化,以替换之前的选择状态

效果图:

 

 

1、进行样式定义

radio_sel_state:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false"
        android:drawable="@drawable/rect_custom_gray" />
    <item android:state_checked="true"
        android:drawable="@drawable/rect_custom_blue" />
</selector>
rect_custom_gray:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/white" />
    <corners android:radius="@dimen/space_60" />
    <stroke android:width="@dimen/space_1" android:color="@color/list_divider"/>

</shape>
rect_custom_blue:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!-- 填充颜色 -->
    <solid android:color="@color/white" />
    <!-- 矩形的圆角半径 -->
    <corners android:radius="@dimen/space_60" />
    <stroke android:width="@dimen/space_1" android:color="@color/colorBlue"/>

</shape>

以上的颜色自己设下就行。

 

2、进行文字颜色的处理

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/gray" android:state_checked="false" />
    <item android:color="@color/colorBlue" android:state_checked="true" />
</selector>

 

3、引用

radio_style:
<style name="radio_style">
        <item name="android:button">@null</item>
        <item name="android:gravity">center</item>
        <item name="android:textColor">@color/gray</item>
        <item name="android:background">@drawable/radio_sel_state</item>
        <item name="android:layout_weight">1</item>
        <item name="android:layout_margin">5dp</item>
    </style>

 

4、使用

<RadioButton
                    android:id="@+id/rb_answer1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:checked="false"
                    android:text="@string/check_yes"
                    android:button="@null"
                    android:textColor="@drawable/radio_text_color_change"
                    style="@style/radio_style_changed"/>

5、结构

 

posted @ 2022-08-20 15:24  jiduoduo  阅读(302)  评论(0编辑  收藏  举报