在android中设置颜色
我正在尝试在 android 中设置 tablerow 的背景颜色,但在引用正确的 int 时遇到问题。下面是代码。我做错了什么吗?背景中出现的颜色是浅灰色。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Trackfolio</string>
<color name="colorWhite">#FFFFFF</color>
<color name="colorBlack">#000000</color>
<color name="colorLightBlue">#6495ED</color>
</resources>
row.setBackgroundColor(R.color.colorLightBlue);
另外有没有办法设置文字颜色?
tv.setTextColor(R.color.colorBlack);
I'm trying to set the background color for a tablerow in android and am having trouble referencing the proper int. Below is the code. Am I doing something wrong? The color that turns up in the background is a light grey.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Trackfolio</string>
<color name="colorWhite">#FFFFFF</color>
<color name="colorBlack">#000000</color>
<color name="colorLightBlue">#6495ED</color>
</resources>
row.setBackgroundColor(R.color.colorLightBlue);
Also is there a way to set the text color?
tv.setTextColor(R.color.colorBlack);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
cfarm54,
您访问颜色的方式是在 R.java gen 文件中获取偏移位置。
你需要像这样访问它们......
cfarm54,
The way you are accessing the colors you are getting the offset location in R.java gen file.
You need to access them like this...
哎呀:愚蠢的我,引用颜色资源时应该使用 tableRow.setBackgroundResource() 。
Oops: dumb me, you should use tableRow.setBackgroundResource() when referecing a color resource.
尝试setBackgroundResource(int color)。使用 setBackgroundColor 会使用 R.color.colorLightBlue 用来引用您定义的颜色的 int,并尝试将其解析为颜色,而不是检索引用的颜色。
Try
setBackgroundResource(int color)
. UsingsetBackgroundColor
uses the int that R.color.colorLightBlue uses to reference your defined color, and tries to parse it as a color, rather than retrieving the referenced color.