点击RadioGroup切换图片

本文介绍了如何在Android中利用RadioGroup实现图片切换效果。首先,定义RadioGroup并设置其属性,然后为每个RadioButton创建不同的selector资源文件,分别定义选中和未选中的图片状态。最后,设置默认选中的RadioButton及各个RadioButton的背景资源,以此实现点击RadioGroup时切换不同图片的功能。
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/wx2"/>--@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是每个引用的都不是一样的






评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值