如何设置在Android的一个TextView的颜色? [英] How to set the color of a TextView in Android?

查看:159
本文介绍了如何设置在Android的一个TextView的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我使用下面的标记string.xml文件

In the string.xml file I use the following tag

<color name="mycolor1">#F5DC49</color>

如果我用

 textview1.setTextColor(Color.CYAN);

它的工作原理,但

it works, but

 textview1.setTextColor(R.color.mycolor1);

是行不通的。

is not working.

我如何使用XML文件中定义的颜色?

How can I use the color defined in the XML file?

推荐答案

TextView.setTextColor()接受一个int再presenting的颜色(如0xFFF5DC49)而不是从XML文件的资源ID。在活动中,你可以这样做:

TextView.setTextColor() takes an int representing the color (eg. 0xFFF5DC49) and not the resource ID from the xml file. In an activity, you can do something like:

   textView1.setTextColor(getResources().getColor(R.color.mycolor))

活动之外,你还需要一个上下文如:

   textView1.setTextColor(context.getResources().getColor(R.color.mycolor))

这篇关于如何设置在Android的一个TextView的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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