Android 以编程方式设置背景颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23517879/
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-08-20 07:13:29  来源:igfitidea点击:

Set Background color programmatically

androidbackground-color

提问by user3274646

I try to set background color programmatically but when I set every one of my colors, the background being black but with any color background being white like the application theme.

我尝试以编程方式设置背景颜色,但是当我设置每一种颜色时,背景为黑色,但任何颜色背景都为白色,就像应用程序主题一样。

View someView = findViewById(R.id.screen);
View root = someView.getRootView();
root.setBackgroundColor(color.white);

Can you see the code?

你能看到代码吗?

回答by Mohanad ALKHOULI

I didn't understand your question ... what do you mean by "when i set every one of my colour"? try this (edit: "#fffff" in original answer changed to "#ffffff"

我不明白你的问题......“当我设置我的每一种颜色时”是什么意思?试试这个(编辑:原始答案中的“#ffffff”改为“#ffffff”

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

回答by Sainath Patwary karnate

you need to use getResources() method, try to use following code

您需要使用 getResources() 方法,请尝试使用以下代码

View someView = findViewById(R.id.screen);
View root = someView.getRootView();
root.setBackgroundColor(getResources().getColor(color.white)); 

Edit::

编辑::

getResources.getColor() is deprecated so, use like below

getResources.getColor() 已弃用,因此请使用如下所示

 root.setBackgroundColor(ContextCompat.getColor(this, R.color.white)); 

回答by Piyush

You can use

您可以使用

 root.setBackgroundColor(0xFFFFFFFF);

or

或者

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

回答by Yoann Hercouet

The previous answers are now deprecated, you need to use ContextCompat.getColorto retrieve the color properly:

以前的答案现已弃用,您需要使用ContextCompat.getColor来正确检索颜色:

root.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.white));

回答by Suragch

If you just want to use some of the predefined Android colors, you can use Color.COLOR(where COLORis BLACK, WHITE, RED, etc.):

如果你只是想用一些预先定义的Android的颜色,你可以使用Color.COLOR(其中COLORBLACKWHITERED等):

myView.setBackgroundColor(Color.GREEN);

Otherwise you can do as others have suggested with

否则你可以按照其他人的建议去做

myView.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.myCustomGreen));

I don't recommend using a hex color directly. You should keep all of your custom colors in colors.xml.

我不建议直接使用十六进制颜色。您应该将所有自定义颜色保存在colors.xml 中

回答by Hatim

This must work:

这必须工作:

you must use getResources().getColor(R.color.WHITE)to get the color resource, which you must add in the colors.xmlresource file

必须使用getResources().getColor(R.color.WHITE)获取颜色资源,必须在colors.xml资源文件中添加

View someView = findViewById(R.id.screen);

someView.setBackgroundColor(getResources().getColor(R.color.WHITE));

回答by Hay Thi

If you save color code in the colors.xml which is under the values folder,then you should call the following:

如果您将颜色代码保存在 values 文件夹下的 colors.xml 中,那么您应该调用以下内容:

root.setBackgroundColor(getResources().getColor(R.color.name));

namemeans you declare in the <color/>tag.

name意味着您在<color/>标签中声明。

回答by Jorge Casariego

In my case it wasn't changing the color because I was setting the color in my xml resource.

就我而言,它没有改变颜色,因为我在我的 xml 资源中设置了颜色

After delete the line that set the color it worked perfectly programmatically

删除设置颜色的行后,它以编程方式完美运行

This is an example I did in a RecyclerView

这是我在 RecyclerView 中做的一个例子

final Drawable drawable = ContextCompat.getDrawable(mContext, R.drawable.ic_icon).mutate();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
    holder.image.setBackground(drawable);
} else {
    holder.image.setBackgroundDrawable(drawable);
}