Android - 以编程方式设置布局背景
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12678949/
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
Android - set layout background programmatically
提问by sethengine
I have noticed that the setBackgroundmethod for the RelativeLayoutobject is targeted for API 16 (Android 4.1) and higher, but my application has the target API 8 and I cannot use it.
我注意到RelativeLayout对象的setBackground方法针对 API 16 (Android 4.1) 及更高版本,但我的应用程序具有目标 API 8,我无法使用它。
Is there any alternative solution for this problem (besides marking the class/method with TargetApi(16) or changing the target API in the manifest)?
Thank you!
这个问题有没有其他解决方案(除了用TargetApi(16)标记类/方法或更改清单中的目标 API)?
谢谢!
Edit: Eclipse was buggy and it showed me the same error for setBackgroundDrawablebut now it seems to work. Thank you for your help.
编辑:Eclipse 有问题,它向我显示了与setBackgroundDrawable相同的错误,但现在它似乎可以工作了。感谢您的帮助。
回答by Eric
Use one of:
使用以下之一:
.setBackgroundColor(int)
(if you're setting it to a color).setBackgroundDrawable(Drawable)
(if you're setting it to aDrawable
type; this is deprecated, and was replaced by.setBackground(Drawable)
).setBackgroundResource(int)
(for setting a resource fromR.java
)
.setBackgroundColor(int)
(如果您将其设置为颜色).setBackgroundDrawable(Drawable)
(如果您将其设置为Drawable
类型;这已被弃用,并被替换为.setBackground(Drawable)
).setBackgroundResource(int)
(用于从 设置资源R.java
)
If you use the second one, make sure to do a conditional check on your API version:
如果您使用第二个,请确保对您的 API 版本进行条件检查:
if (Build.VERSION.SDK_INT >= 16)
view.setBackground(...);
else
view.setBackgroundDrawable(...);
... and mark it with @TargetApi(16)
and @SuppressWarnings("deprecation")
.
...并用@TargetApi(16)
和标记它@SuppressWarnings("deprecation")
。
回答by Raghav Sood
It depends. If you want to set a color as the background, use setBackgroundColor()
. For a Drawable, you can use the now deprecated method setBackgroundDrawable()
for APIs below 16, and setBackground()
for API 16 devices. You can also use setBackgroundResource()
for setting a resource as the background.
这取决于。如果要将颜色设置为背景,请使用setBackgroundColor()
. 对于 Drawable,您可以setBackgroundDrawable()
对低于 16setBackground()
的 API和API 16 设备使用现已弃用的方法。您还可以setBackgroundResource()
用于将资源设置为背景。
Note that while a lot of methods are marked as deprecated, I'm yet to come across one that has actually been removed. So while you could use the deprecated method even in API 16, I'd recommend setting your target API to 16 and using an if else to switch between the methods, depending on the API version the device is running.
请注意,虽然许多方法被标记为已弃用,但我还没有遇到一个实际上已被删除的方法。因此,虽然您甚至可以在 API 16 中使用已弃用的方法,但我建议将您的目标 API 设置为 16 并使用 if else 在方法之间切换,具体取决于设备运行的 API 版本。
回答by Samuel
Use setBackgroundDrawable()instead. It does the same thing, but it's been deprecated since the new setBackground()
method.
请改用setBackgroundDrawable()。它做同样的事情,但自新setBackground()
方法以来它已被弃用。