使用Selector改变TextView的字体颜色textColor的方法

先上Selector文件,名字为singer_fragment_top_text_style.xml,

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/colorPrimaryDark" android:state_selected="true" />
    <item android:color="@color/colorAccent" android:state_selected="false" />
</selector>

有人可能会发现这个selector文件和我们平时在Drawable里面看到的不一样,因为在drawable文件夹中的selector,是不能有android:color属性的,这个文件其实是放在res/color文件夹中的;

应用的时候,只需要对TextView的textColor直接赋值就好了,如下:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/singer_choice_tv"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_marginLeft="12dp"
    android:layout_marginRight="12dp"
    android:background="@drawable/singer_fragment_top_style"
    android:gravity="center"
    android:minWidth="40dp"
    android:text="A"
    android:textColor="@color/singer_fragment_top_text_style"
    android:textSize="18sp" />

注意,对android:textColor赋值时,不是@drawable/xxxxx,而是@color/XXXXX

转载于:https://www.cnblogs.com/yuqf/p/5887970.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值