Android - 以编程方式设置布局背景

set*_*ine 9 eclipse android relativelayout android-layout

我注意到RelativeLayout对象的setBackground方法是针对API 16(Android 4.1)及更高版本的,但我的应用程序具有目标API 8,我无法使用它.

是否有针对此问题的替代解决方案(除了使用TargetApi(16)标记类/方法或更改清单中的目标API)?
谢谢!

编辑:Eclipse是错误的,它向我展示了setBackgroundDrawable的相同错误,但现在它似乎工作.谢谢您的帮助.

Eri*_*ric 30

使用以下之一:

如果您使用第二个,请确保对您的API版本进行条件检查:

if (Build.VERSION.SDK_INT >= 16)
    view.setBackground(...);
else
    view.setBackgroundDrawable(...);
Run Code Online (Sandbox Code Playgroud)

...并用@TargetApi(16)和标记@SuppressWarnings("deprecation").