Android的 - 设置布局背景编程 [英] Android - set layout background programmatically
问题描述
我已经注意到的的setBackground 的方法,为的 RelativeLayout的的对象是针对API 16(安卓4.1)和较高的,但我的应用程序对目标API 8和我不能使用它。
I have noticed that the setBackground method for the RelativeLayout object is targeted for API 16 (Android 4.1) and higher, but my application has the target API 8 and I cannot use it.
有没有针对此问题的任何替代解决方案(除了标识类/方法的 TargetApi 的(16)或改变清单中的目标API)?
谢谢!
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!
修改:Eclipse的是越野车,它给我同样的错误的 setBackgroundDrawable 的,但现在它似乎工作。感谢您的帮助。
Edit: Eclipse was buggy and it showed me the same error for setBackgroundDrawable but now it seems to work. Thank you for your help.
推荐答案
使用的一个:
- <一个href="http://developer.android.com/reference/android/view/View.html#setBackgroundColor%28int%29"><$c$c>.setBackgroundColor(int)$c$c> (如果你将它设置为彩色)
- <一个href="http://developer.android.com/reference/android/view/View.html#setBackgroundDrawable%28android.graphics.drawable.Drawable%29"><$c$c>.setBackgroundDrawable(Drawable)$c$c> (如果你将它设置为
绘制对象
键入;这是pcated德$ P $,和被替换了<一href="http://developer.android.com/reference/android/view/View.html#setBackground%28android.graphics.drawable.Drawable%29"><$c$c>.setBackground(Drawable)$c$c>) - <一个href="http://developer.android.com/reference/android/view/View.html#setBackgroundResource%28int%29"><$c$c>.setBackgroundResource(int)$c$c> (从设置资源
R.java
)
.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
)
如果你使用第二个,确保做一个条件检查您的API版本:
If you use the second one, make sure to do a conditional check on your API version:
if (Build.VERSION.SDK_INT >= 16)
view.setBackground(...);
else
view.setBackgroundDrawable(...);
...和 @TargetApi将其标记为(16)
和 @燮pressWarnings(德precation)
。
这篇关于Android的 - 设置布局背景编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!