Changing colors using LinearLayout and textview in Java (Android)

I'm a relatively new Android Developer. I noticed that it seemed strange to me. I hope someone can explain I have LinearLayout LL

This line of code failed to execute:

ll.setBackgroundColor(R.color.white);

But this line of code is valid:

ll.setBackgroundResource(R.color.white);

I assume it's just because white is defined in my resource However, I also tried to pass 0xFFFFFF in setbackgroundcolor(), but it didn't work

Similar to my textview text, this line of code fails at execution:

text.setTextColor(R.color.white);

I can see my textview, so I know I initialize it correctly (like my LinearLayout, I can also see it) So I think my question comes down to: how do I use LinearLayout correctly Setbackgroundcolor() and textview setTextColor()?

Thank you very much in advance I've read the document and tried to find information online through Google search, but I didn't make any suggestions

Solution

R.color. Whatever is an int. it is automatically generated as a reference to an externally defined (in XML) resource When you call setbackgroundcolor with this integer, it attempts to resolve the value of this int to color Setbackgroundresource expects to get the resource integer passed to it It retrieves externally defined values and applies colors in this way For setbackgroundcolor, try to use a color that contains a full hexadecimal value of alpha, such as 0xFFFFFF (the first two F values are alpha values)

Editor: Mark beat!: P

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>