如何使用setTextColor设置TEXTCOLOR(ColorsStateList颜色) [英] How to set TextColor using setTextColor(ColorsStateList colors)

查看:383
本文介绍了如何使用setTextColor设置TEXTCOLOR(ColorsStateList颜色)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要改变文字颜色,当状态变化(pressed,焦点)...

I need to change text color when state change(pressed, focus)...

如何设置使用ColorsStateList一个TextView的文本颜色?

How to set text color of a TextView using ColorsStateList?

修改,解决了:

textView.setTextColor (new ColorStateList (
   new int [] [] {
      new int [] {android.R.attr.state_pressed},
      new int [] {android.R.attr.state_focused},
      new int [] {}
   },
   new int [] {
      Color.rgb (255, 128, 192),
      Color.rgb (100, 200, 192),
      Color.White
   }
));

<一个href="http://stackoverflow.com/questions/4626415/change-background-color-of-textview-on-click">Change TextView中对点击背景颜色

推荐答案

如果你需要设置code中的颜色(使用ColorStateList),但仍想保留的颜色状态在一个XML,你可能要使用这样的:

If you need to set the colors in code (using ColorStateList), but still want to keep the color states in an XML, you might want to use this:

try {
    XmlResourceParser parser = getResources().getXml(R.color.your_colors);
    ColorStateList colors = ColorStateList.createFromXml(getResources(), parser);
    mText.setTextColor(colors);
} catch (Exception e) {
    // handle exceptions
}

RES /彩色/ your_colors.xml

res/color/your_colors.xml

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

这篇关于如何使用setTextColor设置TEXTCOLOR(ColorsStateList颜色)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆