用的LinearLayout和TextView的在Java中(安卓)改变颜色 [英] Changing Color with LinearLayout and TextView in Java (Android)

查看:240
本文介绍了用的LinearLayout和TextView的在Java中(安卓)改变颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个较新的Andr​​oid开发人员,我看到什么似乎像一个怪胎,我认为我希望有人能解释清楚。我的LinearLayout LL。

I'm a relatively new Android developer and I noticed what seems like an oddity to me that I'm hoping someone can explain. I have LinearLayout ll.

执行时,该行的code失败对我来说:

This line of code fails for me when executed:

ll.setBackgroundColor(R.color.white);

不过这条线code的作品:

However this line of code works:

ll.setBackgroundResource(R.color.white);

我认为它只是因为我有白在我的资源定义。不过,我也试着在setBackgroundColor(通过0XFFFFFF),而不能正常工作之一。

I assume its simply because I have white defined in my resources. However, I've also tried passing 0xFFFFFF in setBackgroundColor() and that doesn't work either.

同样与我的TextView文本此行code执行时失败:

Similarly with my TextView text this line of code fails when executed:

text.setTextColor(R.color.white);

我可以看到我的TextView,所以我知道我正确初始化它(就像我的LinearLayout,我也可以看到)。所以,我想我的问题归结为:?如何正确使用LinearLayout.setBackgroundColor()和TextView.setTextColor()

I can see my TextView so I know I initialized it correctly (like my LinearLayout which I can also see). So I guess my question boils down to: How do I properly use LinearLayout.setBackgroundColor() and TextView.setTextColor() ?

由于一吨提前。我已经通过了文档阅读,并试图通过谷歌搜索来查找信息网上并没有拿出任何东西。

Thanks a ton in advance. I've read through the docs and tried to find information online via googling and haven't come up with anything.

推荐答案

R.color.whatever 是一个int。它会自动为基准,以外部定义(以XML)资源产生的。当你调用 setBackgroundColor 这个整数,它试图解析这个中断的值作为颜色。 setBackgroundResource 期望得到传递给它的资源的整数。它获取外部定义的值,然后应用颜色的方式。至于 setBackgroundColor ,请尝试使用一个完整的十六进制值颜色阿尔法包括,例如, 0xFFFFFFFF的(其中第一两架F值是α值)。

R.color.whatever is an int. It's automatically generated as a reference to an externally defined (in XML) resource. When you call setBackgroundColor with this integer, it's trying to parse this int's value as a Color. setBackgroundResource expects to get a resource integer passed to it. It retrieves the externally defined value, and then applies the color that way. As for setBackgroundColor, try using a full hex value color with alpha included, e.g. 0xFFFFFFFF (where the first two F values are the alpha value).

编辑:马克殴打! :P

Beaten by Mark! :P

这篇关于用的LinearLayout和TextView的在Java中(安卓)改变颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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