1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > Android开发之RadioButton选择改变字体颜色与背景色

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

时间:2020-01-24 04:35:22

相关推荐

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

先看效果图:

方法一:

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

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

shape_financing_text_color.xml

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="/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="/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中引用这个选择器就好了

<RadioGroupandroid:id="@+id/rg_financing_home"android:layout_width="match_parent"android:layout_height="@dimen/dp_45"android:orientation="horizontal"><RadioButtonandroid: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" /><RadioButtonandroid: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" /><RadioButtonandroid: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="/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"

<RadioGroupandroid:id="@+id/rg_financing_home"android:layout_width="match_parent"android:layout_height="@dimen/dp_45"android:orientation="horizontal"><RadioButtonandroid: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" /><RadioButtonandroid: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" /><RadioButtonandroid: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>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。