setBackground 与 setBackgroundDrawable (Android)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11947603/
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
setBackground vs setBackgroundDrawable (Android)
提问by Pijusn
I want to set background drawable of a view. There are two methods for this (as far as I see): setBackground
and setBackgroundDrawable
.
我想设置一个视图的背景可绘制。对此有两种方法(据我所知):setBackground
和setBackgroundDrawable
.
When I use setBackground
, it says it has been added in API level 16but my project's min SDK version is 7. I assume it's not going to work on anything below 16, am I right? But when I use setBackgroundDrawable, it says it's deprecated.
当我使用时setBackground
,它说它已添加到API 级别 16,但我项目的最小 SDK 版本是 7。我认为它不适用于低于 16 的任何内容,对吗?但是当我使用 setBackgroundDrawable 时,它说它已被弃用。
What am I supposed to use?
我应该用什么?
回答by Warpzit
It's deprecated but it still works so you could just use it. But if you want to be completly correct, just for the completeness of it... You'd do something like following:
它已被弃用,但它仍然有效,因此您可以使用它。但是如果你想完全正确,只是为了它的完整性......你会做如下事情:
int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
setBackgroundDrawable();
} else {
setBackground();
}
For this to work you need to set buildTarget api 16 and min build to 7 or something similar.
为此,您需要将 buildTarget api 16 和 min build 设置为 7 或类似的值。
回答by Ludovic
You can use setBackgroundResource()
instead which is in API level 1.
您可以改用setBackgroundResource()
API 级别 1 中的 API。
回答by android developer
seems that currently there is no difference between the 2 functions, as shown on the source code(credit to this post) :
似乎目前这两个函数之间没有区别,如源代码所示(归功于这篇文章):
public void setBackground(Drawable background) {
//noinspection deprecation
setBackgroundDrawable(background);
}
@Deprecated
public void setBackgroundDrawable(Drawable background) { ... }
so it's just a naming decision, similar to the one with fill-parent vs match-parent .
所以这只是一个命名决定,类似于 fill-parent vs match-parent 。
回答by Jose De Gouveia
i know this is an old question but i have a similar situation ,and my solution was
我知道这是一个老问题,但我有类似的情况,我的解决方案是
button.setBackgroundResource( R.drawable.ic_button );
Drawable d = button.getBackground();
and then you can play with the "Drawable", applying color filters, etc
然后你可以玩“Drawable”,应用颜色过滤器等
回答by krawa
Use ViewCompat.setBackground(view, background);
用 ViewCompat.setBackground(view, background);
回答by Emmanuel Francis Narvasa Ramos
you could use setBackgroundResource()
instead i.e. relativeLayout.setBackgroundResource(R.drawable.back);
你可以setBackgroundResource()
改用即relativeLayout.setBackgroundResource(R.drawable.back);
this works for me.
这对我有用。
回答by Geraldo Neto
Now you can use either of those options. And it is going to work in any case. Your color can be a HEX code, like this:
现在您可以使用这些选项中的任何一个。它在任何情况下都会起作用。您的颜色可以是十六进制代码,如下所示:
myView.setBackgroundResource(ContextCompat.getColor(context, Color.parseColor("#FFFFFF")));
A color resource, like this:
一个颜色资源,像这样:
myView.setBackgroundResource(ContextCompat.getColor(context,R.color.blue_background));
Or a custom xml resource, like so:
或自定义 xml 资源,如下所示:
myView.setBackgroundResource(R.drawable.my_custom_background);
Hope it helps!
希望能帮助到你!
回答by Aksel Willgert
Using Android studio 1.5.1 i got the following warnings:
使用 Android studio 1.5.1 我收到以下警告:
Call requires API level 16 (current min is 9): android.view.View#setBackground
and the complaints about deprecation
以及关于弃用的投诉
'setBackgroundDrawable(android.graphics.drawable.Drawable)' is deprecated
Using this format, i got rid of both:
使用这种格式,我摆脱了两者:
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {
//noinspection deprecation
layout.setBackgroundDrawable(drawable);
} else {
layout.setBackground(drawable);
}
回答by user0987
This works for me: View viewis your editText, spinner...etc. And int drawableis your drawable route example (R.drawable.yourDrawable)
这对我有用:视图视图是您的编辑文本、微调器...等。而int drawable是你的 drawable 路线示例(R.drawable.yourDrawable)
public void verifyDrawable (View view, int drawable){
int sdk = Build.VERSION.SDK_INT;
if(sdk < Build.VERSION_CODES.JELLY_BEAN) {
view.setBackgroundDrawable(
ContextCompat.getDrawable(getContext(),drawable));
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
view.setBackground(getResources().getDrawable(drawable));
}
}
回答by baburaoS
Use setBackgroundResource(R.drawable.xml/png)
使用 setBackgroundResource(R.drawable.xml/png)