在 Java (Android) 中使用 LinearLayout 和 TextView 更改颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4715593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 07:40:52  来源:igfitidea点击:

Changing Color with LinearLayout and TextView in Java (Android)

javaandroidandroid-widgetandroid-layout

提问by Rob S.

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.

我是一名相对较新的 Android 开发人员,我注意到对我来说似乎很奇怪的事情,我希望有人能解释一下。我有 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.

我认为这仅仅是因为我在我的资源中定义了白色。但是,我也尝试在 setBackgroundColor() 中传递 0xFFFFFF 并且这也不起作用。

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

与我的 TextView 文本类似,这行代码在执行时失败:

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() ?

我可以看到我的 TextView,所以我知道我正确地初始化了它(就像我也可以看到的 LinearLayout)。所以我想我的问题归结为:如何正确使用 LinearLayout.setBackgroundColor() 和 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.

提前致谢。我通读了文档并尝试通过谷歌搜索在线查找信息,但没有提出任何意见。

回答by Mark G.

Rob,

抢,

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

问题是 setBackgroundColor() 正在寻找颜色对象。所以你需要使用类似下面的行

ll.setBackgroundColor(Color.WHITE);

or

或者

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

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

而 setBackgroundResource 正在寻找用作背景的资源,即您的 res 文件夹中的某些内容。您可以使用它来将背景设置为可绘制或类似的东西。

ll.setBackgroundResource(R.something.mydrawable);

回答by Kevin Coppock

R.color.whateveris an int. It's automatically generated as a reference to an externally defined (in XML) resource. When you call setBackgroundColorwith this integer, it's trying to parse this int's value as a Color. setBackgroundResourceexpects 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).

R.color.whatever是一个整数。它自动生成为对外部定义(在 XML 中)资源的引用。当您setBackgroundColor使用此整数调用时,它会尝试将此整数的值解析为颜色。setBackgroundResource期望获得传递给它的资源整数。它检索外部定义的值,然后以这种方式应用颜色。至于setBackgroundColor,请尝试使用包含 alpha 的完整十六进制值颜色,例如 0xFFFFFFFF(其中前两个 F 值是 alpha 值)。

EDIT: Beaten by Mark! :P

编辑:被马克打败了!:P

回答by ataulm

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).

就我而言,我必须同时使用资源中定义的颜色整数和颜色。尽管它们都是整数,但它们必须具有某种格式才能与 setBackgroundColor(int colorInt) 和 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.

以颜色整数的格式获取 resIds,这样我就可以在任何地方使用 setBackgroundColor。



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:

Android 框架 API 应该已经被注释了:

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