简体   繁体   中英

Changing Color with LinearLayout and TextView in Java (Android)

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.

This line of code fails for me when executed:

ll.setBackgroundColor(R.color.white);

However this line of code works:

ll.setBackgroundResource(R.color.white);

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.

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

text.setTextColor(R.color.white);

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.

Rob,

The problem is that setBackgroundColor() is looking for a color object. So you would need to use something like the line below

ll.setBackgroundColor(Color.WHITE);

or

ll.setBackgroundColor(Color.parseColor("#ffffff"));

whereas setBackgroundResource is looking for a resource to use as the background, ie something in your res folder. You could use that to set the background to a drawable or something of that nature.

ll.setBackgroundResource(R.something.mydrawable);

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, eg 0xFFFFFFFF (where the first two F values are the alpha value).

EDIT: Beaten by Mark! :P

In my case I had to use both Color ints and Colors defined in a resource. Though they were both integers, they had to be of a certain format to work with setBackgroundColor(int colorInt) and setBackgroundResource(int resourceIdOfColor).

I used:

int colorInt = getResources().getColor(R.color.resourceIdOfColor)

to get the resIds in the format of a color int, so then I could use setBackgroundColor everywhere.


You can use annotations to differentiate between the two in your own code:

@ColorInt
private int getColor(@ColorRes int id) {
    return getResources().getColor(id);
}

The Android framework APIs should be annotated already:

public void setBackgroundResource(@DrawableResource int id);
public void setBackgroundColor(@ColorInt int color);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM