使用selector修改TextView中字体的颜色

selector想必大家都用过了,但是在修改字体的颜色的时候还是要细心。
我们在TextView中设置字体颜色一般使用
android:textColor="@color/red"
但是我们在使用selector动态修改字体颜色的时候要使用
[html] view plaincopy
android:color="@color/red"

我遇到这个问题的时候是在TabActivity中,每个Tab在选中的时候修改为蓝色。
tab_item.xml的代码如下:
[html] view plaincopy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_software_tabwidget_item"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >

<ImageView
android:id="@+id/iv_software_tabwidget_icon"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_marginBottom="1dip"
android:layout_marginTop="5dip"
android:scaleType="fitXY" />

<TextView
android:id="@+id/tv_software_tabwidget_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip"
android:textColor="@drawable/software_textcolor"
android:textSize="14dip" />

</LinearLayout>

注意android:textColor="@drawable/software_textcolor",即software_textcolor.xml就是selector,源码如下:
[html] view plaincopy
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >

<item android:state_selected="true" android:color="@color/software_textColor_selected"></item>
<item android:state_selected="false" android:color="@color/software_textColor_unselected"></item>

</selector>

这个文件中就是要注意的地方了,必须使用android:color="@color/software_textColor_selected",不能使用android:textColor属性。
另附color.xml的源码如下:
[html] view plaincopy
<?xml version="1.0" encoding="utf-8"?>
<resources>

<color name="software_textColor_selected">#FF1C94EA</color>
<color name="software_textColor_unselected">#FFDCE0DF</color>

</resources>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你想要在ImageButton的selector同时修改背景颜色和替换背景图片,可以按照以下步骤进行操作: 1. 在drawable文件夹下创建一个selector的xml文件,例如button_selector.xml。 2. 在selector定义不同状态下的背景颜色和背景图片,例如: ``` <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/button_pressed" android:state_pressed="true"/> <item android:drawable="@drawable/button_normal"/> </selector> ``` 在这个例子,当ImageButton被按下时,背景图片会被设置为button_pressed,否则背景图片会被设置为button_normal。 3. 在代码为ImageButton设置selector,并设置背景颜色,例如: ``` ImageButton myButton = findViewById(R.id.my_button); myButton.setBackgroundResource(R.drawable.button_selector); myButton.setBackgroundColor(ContextCompat.getColor(this, R.color.button_color)); ``` 这样,当ImageButton被按下时,背景图片会被替换为button_pressed,并且背景颜色会被设置为button_color。 如果你想要在不同状态下设置不同的背景颜色,可以在selector添加类似下面的内容: ``` <item android:color="@color/button_pressed_color" android:state_pressed="true"/> <item android:color="@color/button_normal_color"/> ``` 然后在代码使用下面的语句设置背景颜色: ``` myButton.setBackgroundColor(ContextCompat.getColorStateList(this, R.color.button_color)); ``` 这样,当ImageButton被按下时,背景颜色会被设置为button_pressed_color,否则背景颜色会被设置为button_normal_color
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值