java 如何删除(常规)视图的背景(Android)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5851077/
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
How to remove the background of a (regular) view (Android)
提问by Tal A.
I have the following code:
我有以下代码:
View view = new View(this);
view.setBackgroundDrawable(...);
...
And here I want to remove that background.
Just turn it back as it was before.
在这里我想删除那个背景。
像以前一样把它转回来。
I tried these and failed:
我尝试了这些但失败了:
view.setBackgroundDrawable(null);
view.setBackgroundColor(0xFF000000);
view.setBackgroundColor(0x00000000);
Any more ideas?
还有更多想法吗?
回答by tacone
view.setBackgroundDrawable(null);
should work.
view.setBackgroundDrawable(null);
应该管用。
You may try one of these:
您可以尝试以下方法之一:
v.setBackgroundColor(Color.WHITE);
//or
v.setBackgroundColor(Color.parseColor("#ff0000")); //whatever color
Make sure the view you're applying the background to is the correct instance.
确保您应用背景的视图是正确的实例。
回答by dmon
That's because view.setBackgroundColor(int)
expects a color resource not an "actual" integer value. Try declaring it in your colors.xml, see this. However, I'm not quite sure what you mean by "removing" the background. If you want it to have the original value, then I suggest you store the original drawable (using getBackground()
) somewhere. Otherwise you will most likely lose formatting, since most default backgrounds in Android are Drawable resources (selectors), not simple colors.
那是因为view.setBackgroundColor(int)
期望颜色资源不是“实际”整数值。尝试在您的 color.xml 中声明它,请参阅此。但是,我不太确定您所说的“删除”背景是什么意思。如果您希望它具有原始值,那么我建议您将原始可绘制对象(使用getBackground()
)存储在某处。否则你很可能会丢失格式,因为 Android 中的大多数默认背景都是 Drawable 资源(选择器),而不是简单的颜色。