设置Button的字体颜色状态选择器

以前项目中经常使用的就是Button的状态背景选择器了,但是现在突然遇到一个字体颜色也要改变的需求,按照以前的方法时出现了一些问题,现在就把正确的解决方案写一下

整个需求需要3步来完成

1> 创建text_color_selector.xml

2>设置颜色

3>在button里面引用这个样式


第一步,在res文件夹下面创建drawable文件夹,在drawable里面创建text_color_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="false" android:state_enabled="true" android:state_pressed="false"
        android:color="@drawable/red" />
    <item android:state_enabled="false" android:color="@drawable/gray" />
    <item android:state_pressed="true" android:color="@drawable/green" />
    <item android:state_focused="true"  android:color="@drawable/red" />
</selector>

2>设置颜色
在values创建background。xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <drawable name="red">#ff0000</drawable>
    <drawable name="black">#000000</drawable>
    <drawable name="green">#0f0</drawable>
    <drawable name="gray">#ccc</drawable>
</resources>

3>在Button里面吧选择器设置给textColor


  <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/button_text"
        android:textColor="@drawable/text_color_selector"/>

OK搞定


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值