Android学习之控件不同状态文字显示不同颜色

本文介绍了两种在Android中使控件在不同状态下显示不同颜色的方法。方式一是通过在TextView的style中设置`textColor`为`@drawable/textcolor_yellow_selector`,在selector文件中定义不同状态的颜色。方式二是使用ColorStateList,通过资源XML文件定义颜色状态,并在代码中设置Button的文字颜色。
摘要由CSDN通过智能技术生成

方式一:

第一要选择的控件

<TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/close_time_display"
      android:layout_marginRight="20dp"
      android:text="@string/default_time"
      style="@style/item_content_text_style"/>

 

style是自定义的风格,对应的xml文件如下:

    <style name="item_content_text_style">
        <item name="android:textSize">26sp</item>
        <item name="android:duplicateParentState">true</item>
        <item name="android:textColor">@drawable/textcolor_yellow_selector</item>
    </style>

 

textColor中的textcolor_yellow_selector如下

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

 

实现方式二:ColorStateList文字变色


 

API

 

Windows平台VC,对于不同的按钮状态,采用不同的颜色显示文字,实现起来比较复杂,一般都得自绘按钮。但是Android里面实现起来非常方便。 

我们首先添加一个ColorStateList资源XML文件,XML文件保存在res/color/button_text.xml:

Java代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值