Android 如何设置视图的背景颜色

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

How to set background color of a View

androidviewcolorsbackgroundset

提问by Peter vdL

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.

谢谢。

回答by rainhut

You made your button transparent. The first byte is the alpha.

你让你的按钮透明。第一个字节是 alpha。

Try v.setBackgroundColor(0xFF00FF00);

尝试 v.setBackgroundColor(0xFF00FF00);

回答by EddieB

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.* 试验不同的效果。

回答by Jorgesys

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:

使用 Color.GREEN 常量将背景设置为绿色:

v.setBackgroundColor(Color.GREEN);

Set background to green defining in Colors.xml

Colors.xml 中将背景设置为绿色

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

回答by Vipul Patel

You can set the hex-color to any resource with:

您可以使用以下命令将十六进制颜色设置为任何资源:

View.setBackgroundColor(Color.parseColor("#e7eecc"));

回答by malte kosian

// 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 值为零时,颜色将不可见。

The correct hex value is 0xFF00FF00for full opacity green. Any Alpha value between 00 and FF would cause transparency.

正确的十六进制值适用0xFF00FF00于完全不透明的绿色。任何介于 00 和 FF 之间的 Alpha 值都会导致透明度。

回答by CommonsWare

and what is the correct way to change the background color on any View?

在任何视图上更改背景颜色的正确方法是什么?

On anyView? What you have is correct, though you should drop the invalidate()call.

任何View?你有什么是正确的,虽然你应该挂断invalidate()电话。

However, some Viewsalready 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.xmlin 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.

然而,有些人Views已经有了背景。Button例如,A已经有一个背景:按钮本身的表面。这个背景是一个StateListDrawable,你可以android-2.1/data/res/drawable/btn_default.xml在你的 Android SDK 安装中找到它。反过来,这指的是一堆九个补丁位图图像,可用于多种密度。您需要克隆和修改所有这些来实现您的绿色目标。

In short, you will be better served finding another UI pattern rather than attempting to change the background of a Button.

简而言之,您最好找到另一种 UI 模式,而不是尝试更改Button.

回答by JustinB

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:

要设置在屏幕上显示的第一种颜色,您也可以在相关的 layout.xml(更好的设计)中通过将此属性添加到相关的视图中来进行设置:

android:background="#FF00FF00"

回答by Long Rainbow

try to add:

尝试添加:

setBackgroundColor(Color.parseColor("#FF0000"));

回答by Vostro

I use at API min 16 , target 23

我在 API min 16 使用,目标为 23

Button WeekDoneButton = (Button) viewWeeklyTimetable.findViewById(R.id.week_done_button);

WeekDoneButton.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.colorAccent));

回答by Gianluca Demarinis

mButton.setBackgroundColor(getResources().getColor(R.color.myColor));