【发布时间】:2011-02-06 04:36:51
【问题描述】:
我正在使用 Eclipse 开发一个 Android 项目。我想使用我在 res/values/colors.xml 中定义的一种颜色来更改 TextView 的背景颜色。这些颜色都可以通过使用 R.color.color_name 获得。
我的问题是这根本行不通。更改为我定义的颜色之一总是将 TextView 的背景设置为其默认颜色,在本例中为黑色。如果我使用 Java 的一种内置颜色,它可以正常工作。我认为这是一个颜色定义问题,涉及到我如何在 XML 中实际定义颜色,但我不确定。
// This works:
weight1.setBackgroundColor(Color.BLACK);
// This does not work:
weight2.setBackgroundColor(R.color.darkgrey);
// Color Definition: (this is in a separate xml file, not in my Java code)
<color name = "darkgrey">#A9A9A9</color>
【问题讨论】: