Android开发之RadioButton选择改变字体颜色与背景色

先看效果图:

方法一:

公司理财项目里面用到的,当选中radioButton后要改变背景色和自提颜色,试了好几次这么都不行。

于是想到了selecter背景选择器,可是怎么写都不对。终于找到一种成功的写法了。下面是字体颜色改变的方法

shape_financing_text_color.xml

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

下面是整个radiobutton背景颜色改变的方法

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true">
        <shape>
            <solid android:color="@color/colorMain" />
            <stroke android:width="@dimen/dp_1" android:color="@color/colorKeyBoard" />
        </shape>
    </item>
    <item android:state_checked="false">
        <shape>
            <stroke android:width="@dimen/dp_1" android:color="@color/colorKeyBoard" />
        </shape>
    </item>
</selector>

如何引用呢?

很简单如下:

在textcolor中引用这个选择器就好了

  <RadioGroup
        android:id="@+id/rg_financing_home"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp_45"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/rb_financing_home"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/shape_financing_bg"
            android:button="@null"
            android:checked="true"
            android:gravity="center"
            android:text="首页"
            android:textColor="@drawable/shape_financing_text_color" />

        <RadioButton
            android:id="@+id/rb_financing_money"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="0.5dp"
            android:layout_marginRight="0.5dp"
            android:layout_weight="1"
            android:background="@drawable/shape_financing_bg"
            android:button="@null"
            android:gravity="center"
            android:text="资产"
            android:textColor="@drawable/shape_financing_text_color" />

        <RadioButton
            android:id="@+id/rb_financing_mine"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/shape_financing_bg"
            android:button="@null"
            android:gravity="center"
            android:text="我的"
            android:textColor="@drawable/shape_financing_text_color" />
    </RadioGroup>

方法二:

看小效果图:

先创建selecter

放在res/color里面

没有color文件夹需要自己创建color文件夹哈

text_color.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#66ff66" android:state_checked="true" />
    <item android:color="#00ffff" android:state_checked="false" />
</selector>

然后在xml中引用即可

android:textColor="@color/text_color"

  <RadioGroup
        android:id="@+id/rg_financing_home"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp_45"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/rb_financing_home"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/shape_financing_bg"
            android:button="@null"
            android:checked="true"
            android:gravity="center"
            android:text="首页"
            android:textColor="@color/text_color" />

        <RadioButton
            android:id="@+id/rb_financing_money"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="0.5dp"
            android:layout_marginRight="0.5dp"
            android:layout_weight="1"
            android:background="@drawable/shape_financing_bg"
            android:button="@null"
            android:gravity="center"
            android:text="资产"
            android:textColor="@color/text_color" />

        <RadioButton
            android:id="@+id/rb_financing_mine"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/shape_financing_bg"
            android:button="@null"
            android:gravity="center"
            android:text="我的"
            android:textColor="@color/text_color" />
    </RadioGroup>
发布了191 篇原创文章 · 获赞 105 · 访问量 36万+

猜你喜欢

转载自blog.csdn.net/xiayiye5/article/details/91127352
今日推荐