如何删除(常规)视图的背景 (Android)
我有以下代码:
View view = new View(this);
view.setBackgroundDrawable(...);
...
在这里我想删除该背景。
只需将其转回原来的样子即可。
我尝试了这些但失败了:
view.setBackgroundDrawable(null);
view.setBackgroundColor(0xFF000000);
view.setBackgroundColor(0x00000000);
还有更多的想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
view.setBackgroundDrawable(null);
应该可以工作。您可以尝试以下方法之一:
确保您要应用背景的视图是正确的实例。
view.setBackgroundDrawable(null);
should work.You may try one of these:
Make sure the view you're applying the background to is the correct instance.
这是因为 view.setBackgroundColor(int) 需要颜色资源而不是“实际”整数值。尝试在 color.xml 中声明它,请参阅此 。但是,我不太确定“删除”背景是什么意思。如果您希望它具有原始值,那么我建议您将原始可绘制对象(使用
getBackground()
)存储在某处。否则,您很可能会丢失格式,因为 Android 中的大多数默认背景都是可绘制资源(选择器),而不是简单的颜色。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 (usinggetBackground()
) somewhere. Otherwise you will most likely lose formatting, since most default backgrounds in Android are Drawable resources (selectors), not simple colors.