字体颜色变化效果

字体的颜色变化效果跟按钮的背景变化效果差不多,都是定义一个selector选择器,只是在定义时,无法ctrl+/无法关联出color的属性

耽误了一小段时间。

首先定义在value目录下创建一个color.xml的文件定义颜色:

<resources>
        <color name="total">#8be18f</color>
        <color name="today">#ff9c00</color>
        <color name="month">#0096ff</color>
        
        <color name="white">#ffffff</color>
        <color name="black">#000000</color>
        
        <drawable name="white">#ffffff</drawable>
        <drawable name="black">#000000</drawable>
</resources>

然后创建drawable文件夹,创建text_color.xml 名字任意取

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

注意在定义selector选择器时,我ctrl+/无法关联出color的属性,这里浪费了一点时间

然后在文本颜色定义时引用这个颜色选择器

        <TextView
            android:id="@+id/about_us"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/about_us_avatar"
            android:layout_marginLeft="8dp" 
            android:layout_centerVertical="true"          
            android:gravity="center"
            android:text="@string/setting_aboutus"
            android:textSize="15sp"
            android:textColor="@drawable/text_color"/>

背景同样这样设置,就能够达到点击文本时,背景图片和文字颜色同时发生改变的效果。

这里要注意一下,如果控件是TextView并且使用了drawableTop之类的属性来设置图片和文字,好像无法同时设置图片和文字颜色改变的效果

也就是drawableTop使用背景选择器,文字颜色使用颜色选择器,这样无法达到点击时同时改变图片背景和文字颜色的效果。必须将图片和

文字放在不同的控件中,然后分别使用背景选择器和文字颜色选择器。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值