setBackground 与 setBackgroundDrawable (Android) [英] setBackground vs setBackgroundDrawable (Android)

查看:38
本文介绍了setBackground 与 setBackgroundDrawable (Android)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置一个视图的背景可绘制.对此有两种方法(据我所知):setBackgroundsetBackgroundDrawable.

I want to set background drawable of a view. There are two methods for this (as far as I see): setBackground and setBackgroundDrawable.

当我使用 setBackground 时,它说它已被添加到 API 级别 16 但我项目的最小 SDK 版本是 7.我认为它不会在任何事情上工作低于 16 岁,我说得对吗?但是当我使用 setBackgroundDrawable 时,它​​说它已被弃用.

When I use setBackground, it says it has been added in API level 16 but 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.

我应该用什么?

推荐答案

它已被弃用,但它仍然有效,因此您可以直接使用它.但是如果你想完全正确,只是为了它的完整性......你会做如下的事情:

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();
}

为此,您需要将 buildTarget api 16 和 min build 设置为 7 或类似的值.

For this to work you need to set buildTarget api 16 and min build to 7 or something similar.

这篇关于setBackground 与 setBackgroundDrawable (Android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆