Android开发之RadioButton选择改变字体颜色与背景色
生活随笔
收集整理的這篇文章主要介紹了
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中引用這個選擇器就好了
<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="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"
<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>?
總結
以上是生活随笔為你收集整理的Android开发之RadioButton选择改变字体颜色与背景色的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 维数约减--Dimensionality
- 下一篇: Win10软件出现乱码怎么解决 电脑一些