如何设置在Android的一个TextView的颜色?如何设置、颜色、Android、TextView

2023-09-05 07:34:35 作者:我旳世界"妳不懂

在我使用下面的标记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))