Android RadioButton设置选择器(控件设选择器)可以做类似底部导航栏

本文介绍了如何使用RadioButton控件设置选择器,通过选择器改变样式,实现类似底部导航栏的效果。在Android Studio 2.3.3中,可以通过创建Drawable资源文件来定义选择器,如draw_top_image.xml(图片选择器)和text_color.xml(文本颜色选择器),并应用于布局文件中。

一言不合就上图

这里写图片描述
一般的控件都可以设置选择器:即选择改变样式
包括textView,imageView,button等等…
首先创建选择器,在src—main—res—drawable—
这里写图片描述
右键new—Drawable resource file:
这里写图片描述
创建起名字:
这里写图片描述
//这是图片选择器:draw_top_image.xml

//checked选中(true)一种状态的图片样式
    <item android:drawable="@mipmap/ic_launcher_round" android:state_checked="true"></item>
//checked未选中(false)一种状态的图片样式
    <item android:drawable="@mipmap/ic_launcher" android:state_checked="false"></item>

//这是文本颜色选择器:text_color.xml

//checked选中(true)一种状态的图片样式
    <item android:color="@color/colorAccent" android:state_checked="true"></item>
//checked未选中(false)一种状态的图片样式
    <item android:color="@color/colorPrimary" android:state_checked="false"></item>

//布局文件:

<RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/shou"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:layout_weight="1"//权重
            android:button="@null"//去圆圈
            android:drawableTop="@drawable/draw_top_image"//图片选择器在这里加
            android:gravity="center_horizontal"//文字居中
            android:text="首页"
            android:textColor="@drawable/text_color"//文字的颜色选择器在这里加
             />

        <RadioButton
            android:id="@+id/fen"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:layout_weight="1"
            android:button="@null"
            android:drawableTop="@drawable/draw_top_image"
            android:gravity="center_horizontal"
            android:text="分类"
            android:textColor="@drawable/text_color" />

        <RadioButton
            android:id="@+id/gou"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:layout_weight="1"
            android:button="@null"
            android:drawableTop="@drawable/draw_top_image"
            android:gravity="center_horizontal"
            android:text="购物"
            android:textColor="@drawable/text_color" />

        <RadioButton
            android:id="@+id/wode"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:layout_weight="1"
            android:button="@null"

            android:drawableTop="@drawable/draw_top_image"
            android:gravity="center_horizontal"
            android:text="个人"
            android:textColor="@drawable/text_color" />
    </RadioGroup>

完事儿

强调几点:
1我的Android studio是2.3.3版本,SDK下-v7:26.+’

compile 'com.android.support:appcompat-v7:26.+' 
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值