I'm trying to set the background color of a View (in this case a Button).
我正在尝试设置视图的背景颜色(在本例中是按钮)。
I use this code:
我用这段代码:
// set the background to green
v.setBackgroundColor(0x0000FF00 );
v.invalidate();
It causes the Button to disappear from the screen. What am I doing wrong, and what is the correct way to change the background color on any View?
它使按钮从屏幕上消失。我做错了什么?在任何视图上改变背景颜色的正确方法是什么?
Thanks.
谢谢。
13 个解决方案
#1
253
You made your button transparent. The first byte is the alpha.
你让你的按钮透明了。第一个字节是。
Try v.setBackgroundColor(0xFF00FF00);
尝试v.setBackgroundColor(0 xff00ff00);
#2
131
When you call setBackgoundColor it overwrites/removes any existing background resource, including any borders, corners, padding, etc. What you want to do is change the color of the existing background resource...
当您调用setBackgoundColor时,它会覆盖/删除任何现有的背景资源,包括任何边框、边角、填充等等。
View v;
v.getBackground().setColorFilter(Color.parseColor("#00ff00"), PorterDuff.Mode.DARKEN);
Experiment with PorterDuff.Mode.* for different effects.
与PorterDuff.Mode实验。*不同的效果。
#3
92
Several choices to do this...
有几个选择可以做到这一点……
Set background to green:
格林:设置背景
v.setBackgroundColor(0x00FF00);
Set background to green with Alpha:
设置背景为绿色,带有Alpha:
v.setBackgroundColor(0xFF00FF00);
Set background to green with Color.GREEN constant:
将背景设置为绿色和颜色。绿色常数:
v.setBackgroundColor(Color.GREEN);
Set background to green defining in Colors.xml
在Colors.xml中将背景设置为green定义
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="myGreen">#00FF00</color>
<color name="myGreenWithAlpha">#FF00FF00</color>
</resources>
and using:
和使用:
v.setBackgroundResource(R.color.myGreen);
and:
和:
v.setBackgroundResource(R.color.myGreenWithAlpha);
or the longer winded:
或喘气的时间越长:
v.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.myGreen));
and:
和:
v.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.myGreenWithAlpha));
#4
32
You can set the hex-color to any resource with:
您可以将hex颜色设置为任何资源:
View.setBackgroundColor(Color.parseColor("#e7eecc"));
#5
15
// set the background to green
v.setBackgroundColor(0x0000FF00 );
v.invalidate();
The code does not set the button to green. Instead, it makes the button totally invisible.
代码没有将按钮设置为绿色。相反,它使按钮完全不可见。
Explanation: the hex value of the color is wrong. With an Alpha value of zero, the color will be invisible.
说明:颜色的十六进制值是错误的。当Alpha值为0时,颜色是不可见的。
The correct hex value is 0xFF00FF00
for full opacity green. Any Alpha value between 00 and FF would cause transparency.
对于完全不透明的绿色,正确的十六进制值是0xFF00FF00。任何在00和FF之间的Alpha值都将导致透明度。
#6
11
and what is the correct way to change the background color on any View?
改变任何视图背景颜色的正确方法是什么?
On any View
? What you have is correct, though you should drop the invalidate()
call.
在任何视图?您所拥有的是正确的,但是应该放弃invalidate()调用。
However, some Views
already have backgrounds. A Button
, for example, already has a background: the face of the button itself. This background is a StateListDrawable
, which you can find in android-2.1/data/res/drawable/btn_default.xml
in your Android SDK installation. That, in turn, refers to a bunch of nine-patch bitmap images, available in multiple densities. You would need to clone and modify all of that to accomplish your green goals.
然而,一些观点已经有了背景。例如,按钮已经有了背景:按钮本身的外观。这个背景是一个StateListDrawable,您可以在android-2.1/data/res/drawable/btn_default中找到它。在您的Android SDK安装中安装xml。反过来,这又指的是一堆9个补丁的位图图像,具有多种密度。你需要克隆和修改所有这些来实现你的绿色目标。
In short, you will be better served finding another UI pattern rather than attempting to change the background of a Button
.
简而言之,您最好找到另一个UI模式,而不是尝试更改按钮的背景。
#7
9
For setting the first color to be seen on screen, you can also do it in the relevant layout.xml (better design) by adding this property to the relevant View:
要设置屏幕上看到的第一种颜色,您还可以在相关的布局中进行设置。将此属性添加到相关视图的xml(更好的设计):
android:background="#FF00FF00"
#8
8
try to add:
尝试添加:
setBackgroundColor(Color.parseColor("#FF0000"));
#9
5
I use at API min 16 , target 23
我在API最小16,目标23
Button WeekDoneButton = (Button) viewWeeklyTimetable.findViewById(R.id.week_done_button);
WeekDoneButton.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.colorAccent));
#10
3
This question talks about changing the background color of a view. In one of the answers, the person explains how to change the background color during runtime. Obviously you are going to look into how to modify other objects on the screen, but this should give you a good start by at least allowing you to modify the background color of the view on button click.
这个问题是关于改变一个视图的背景颜色。在其中一个答案中,这个人解释如何在运行时更改背景颜色。显然,您将研究如何在屏幕上修改其他对象,但这应该会给您一个良好的开端,至少允许您修改按钮单击的视图的背景颜色。
#11
3
You can simple use :
你可以简单地使用:
view.setBackgroundColor(Color.parseColor("#FFFFFF"));
#12
3
mButton.setBackgroundColor(getResources().getColor(R.color.myColor));
#13
1
Stating with Android 6 use ContextCompact
使用Android 6说明使用ContextCompact。
view.setBackgroundColor( ContextCompat.getColor(this, R.color.your_color));
#1
253
You made your button transparent. The first byte is the alpha.
你让你的按钮透明了。第一个字节是。
Try v.setBackgroundColor(0xFF00FF00);
尝试v.setBackgroundColor(0 xff00ff00);
#2
131
When you call setBackgoundColor it overwrites/removes any existing background resource, including any borders, corners, padding, etc. What you want to do is change the color of the existing background resource...
当您调用setBackgoundColor时,它会覆盖/删除任何现有的背景资源,包括任何边框、边角、填充等等。
View v;
v.getBackground().setColorFilter(Color.parseColor("#00ff00"), PorterDuff.Mode.DARKEN);
Experiment with PorterDuff.Mode.* for different effects.
与PorterDuff.Mode实验。*不同的效果。
#3
92
Several choices to do this...
有几个选择可以做到这一点……
Set background to green:
格林:设置背景
v.setBackgroundColor(0x00FF00);
Set background to green with Alpha:
设置背景为绿色,带有Alpha:
v.setBackgroundColor(0xFF00FF00);
Set background to green with Color.GREEN constant:
将背景设置为绿色和颜色。绿色常数:
v.setBackgroundColor(Color.GREEN);
Set background to green defining in Colors.xml
在Colors.xml中将背景设置为green定义
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="myGreen">#00FF00</color>
<color name="myGreenWithAlpha">#FF00FF00</color>
</resources>
and using:
和使用:
v.setBackgroundResource(R.color.myGreen);
and:
和:
v.setBackgroundResource(R.color.myGreenWithAlpha);
or the longer winded:
或喘气的时间越长:
v.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.myGreen));
and:
和:
v.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.myGreenWithAlpha));
#4
32
You can set the hex-color to any resource with:
您可以将hex颜色设置为任何资源:
View.setBackgroundColor(Color.parseColor("#e7eecc"));
#5
15
// set the background to green
v.setBackgroundColor(0x0000FF00 );
v.invalidate();
The code does not set the button to green. Instead, it makes the button totally invisible.
代码没有将按钮设置为绿色。相反,它使按钮完全不可见。
Explanation: the hex value of the color is wrong. With an Alpha value of zero, the color will be invisible.
说明:颜色的十六进制值是错误的。当Alpha值为0时,颜色是不可见的。
The correct hex value is 0xFF00FF00
for full opacity green. Any Alpha value between 00 and FF would cause transparency.
对于完全不透明的绿色,正确的十六进制值是0xFF00FF00。任何在00和FF之间的Alpha值都将导致透明度。
#6
11
and what is the correct way to change the background color on any View?
改变任何视图背景颜色的正确方法是什么?
On any View
? What you have is correct, though you should drop the invalidate()
call.
在任何视图?您所拥有的是正确的,但是应该放弃invalidate()调用。
However, some Views
already have backgrounds. A Button
, for example, already has a background: the face of the button itself. This background is a StateListDrawable
, which you can find in android-2.1/data/res/drawable/btn_default.xml
in your Android SDK installation. That, in turn, refers to a bunch of nine-patch bitmap images, available in multiple densities. You would need to clone and modify all of that to accomplish your green goals.
然而,一些观点已经有了背景。例如,按钮已经有了背景:按钮本身的外观。这个背景是一个StateListDrawable,您可以在android-2.1/data/res/drawable/btn_default中找到它。在您的Android SDK安装中安装xml。反过来,这又指的是一堆9个补丁的位图图像,具有多种密度。你需要克隆和修改所有这些来实现你的绿色目标。
In short, you will be better served finding another UI pattern rather than attempting to change the background of a Button
.
简而言之,您最好找到另一个UI模式,而不是尝试更改按钮的背景。
#7
9
For setting the first color to be seen on screen, you can also do it in the relevant layout.xml (better design) by adding this property to the relevant View:
要设置屏幕上看到的第一种颜色,您还可以在相关的布局中进行设置。将此属性添加到相关视图的xml(更好的设计):
android:background="#FF00FF00"
#8
8
try to add:
尝试添加:
setBackgroundColor(Color.parseColor("#FF0000"));
#9
5
I use at API min 16 , target 23
我在API最小16,目标23
Button WeekDoneButton = (Button) viewWeeklyTimetable.findViewById(R.id.week_done_button);
WeekDoneButton.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.colorAccent));
#10
3
This question talks about changing the background color of a view. In one of the answers, the person explains how to change the background color during runtime. Obviously you are going to look into how to modify other objects on the screen, but this should give you a good start by at least allowing you to modify the background color of the view on button click.
这个问题是关于改变一个视图的背景颜色。在其中一个答案中,这个人解释如何在运行时更改背景颜色。显然,您将研究如何在屏幕上修改其他对象,但这应该会给您一个良好的开端,至少允许您修改按钮单击的视图的背景颜色。
#11
3
You can simple use :
你可以简单地使用:
view.setBackgroundColor(Color.parseColor("#FFFFFF"));
#12
3
mButton.setBackgroundColor(getResources().getColor(R.color.myColor));
#13
1
Stating with Android 6 use ContextCompact
使用Android 6说明使用ContextCompact。
view.setBackgroundColor( ContextCompat.getColor(this, R.color.your_color));