RadioGroup 中按钮背景图片点击切换用选择器实现

本文介绍如何在RadioGroup中通过设置选择器实现按钮背景图片在点击时的切换效果。具体做法是在RadioButton的background属性中引用不同的select.xml选择器文件,每个文件根据选中和未选中的状态定义不同的背景颜色。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.定义你写的RadioGroup控件-----例如下:
<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_height="40dp">

<RadioButton
android:id="@+id/home_button"
android:button="@null"
android:checked="true"
android:gravity="center"
android:background="@drawable/home_selector"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
/>
<RadioButton
android:id="@+id/voide_button"
android:button="@null"
android:gravity="center"
android:background="@drawable/voide_selector"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
/>
<RadioButton
android:id="@+id/headitem_button"
android:button="@null"
android:gravity="center"
android:background="@drawable/head_selector"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
/>
<RadioButton
android:id="@+id/my_button"
android:button="@null"
android:gravity="center"
android:background="@drawable/my_selector"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
/>
</RadioGroup>

2.在drawable 下 右键 New drawable resource file 起一个文件名 File Name 下面 Root element 选择 selector类型
(这里每一个RadioButton都需要引用一个,以上方今日头条为例,所以需要建4个)-----每个里边放你的一个RadioButton对应的一个红色点击的图片和未点击的白色图片

里边代码如下------例: 需要写与上面Radiobutton对应的个数,例我上面写了四个RadioButton 我这里也要写4个对应的selector

解释一下:参考 第一个<item android:state_checked="true" android:drawable="@drawable/yx2"/>--@drawable/yx2是选中时的图片
第二个<item android:state_checked="true" android:drawable="@drawable/yx2"/>--@drawable/wx2是选中时的图片
第三个<item android:drawable="@drawable/yx2"/>--@drawable/wx2是默认的图片

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

3. 3.1:给第一个默认展示的RadioButton设置 android:checked="true"默认选中 

3.2:给每个RadioButton设置对应select.xml 代码:android:background="@drawable/my_selector" (注)这里的my_selector是每个引用的都不是一样的


=====================简单案例============================

drawer下选择器文件

 <!--根据选中和未选中的状态,去设置背景颜色的图片-->
    <item android:state_checked="true" android:drawable="@color/colorAccent"/>
    <item android:state_checked="false" android:drawable="@color/colorPrimary"/>
    <item android:drawable="@color/colorPrimary"/>

colors文件下:

<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
</resources>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值