97

所以我的 sdk 从 15 变为 21,当我打电话时setBackgroundDrawable(),Android Studio 告诉我它已被弃用。

我想绕过它使用:

int sdk = android.os.Build.VERSION.SDK_INT;

if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
    layout.setBackgroundDrawable(getResources().getDrawable(R.drawable.img_wstat_tstorm));
} else {
    layout.setBackground(getResources().getDrawable(R.drawable.img_wstat_tstorm));
}

但是,我在“setBackground()”处收到错误。

那么,你会如何处理呢?

4

11 回答 11

114

这是一个有趣的话题。显然,您这样做的方式是正确的。这实际上只是一个命名决定的改变。正如这个答案所指出的,setBackground()只需调用setBackgroundDrawable()

public void setBackground(Drawable background) {
    //noinspection deprecation
    setBackgroundDrawable(background);
}

@Deprecated
public void setBackgroundDrawable(Drawable background) { ... }

您可以查看此线程以获取有关所有这些的更多信息。

于 2014-11-26T04:28:53.000 回答
30

也许您可以尝试以下方法:

setBackgroundResource(R.drawable.img_wstat_tstorm);
于 2015-04-20T14:22:42.433 回答
18

这很有趣,因为该方法已被弃用,但如果您查看 Android 源代码,您会发现:

   /**
     * Set the background to a given Drawable, or remove the background. If the
     * background has padding, this View's padding is set to the background's
     * padding. However, when a background is removed, this View's padding isn't
     * touched. If setting the padding is desired, please use
     * {@link #setPadding(int, int, int, int)}.
     *
     * @param background The Drawable to use as the background, or null to remove the
     *        background
     */
    public void setBackground(Drawable background) {
        //noinspection deprecation
        setBackgroundDrawable(background);
    }
于 2015-01-30T18:32:22.573 回答
14

截至 2018 年 8 月 15 日正确

使用支持库

Drawable drawable = ResourcesCompat.getDrawable(getResources(), drawableRes, null);
ViewCompat.setBackground(layout, drawable);
于 2018-08-15T11:01:38.770 回答
8

您收到错误是因为 getResources().getDrawable() 将 id (int) 而不是可绘制对象作为其参数。试试这个:

layout.setBackground(getResources().getDrawable(R.id.img_wstat_tstorm));

于 2014-11-26T04:32:48.897 回答
5

用这个:

myView.background = ContextCompat.getDrawable(context, R.id.my_drawable)
于 2019-12-02T11:04:37.730 回答
4
//Java
view.setBackground(ActivityCompat.getDrawable(context, R.drawable.bg))

//Kotlin 
view.background = ActivityCompat.getDrawable(context, R.drawable.bg)
于 2018-03-29T09:19:22.993 回答
3

这在我的情况下是正确的 解决这个问题

 imageView.setBackgroundResource(images[productItem.getPosition()]);
于 2016-04-12T11:45:04.100 回答
2

截至 2018 年 11 月 23 日正确

科特林:

view.background = resources.getDrawable(R.drawable.ic_image,theme)

如果包含 Theme 参数。

于 2018-11-23T14:08:10.290 回答
0

我正在使用 minSdkVersion 16 和 targetSdkVersion 23 以下对我有用,它使用 ContextCompat.getDrawable(context, R.drawable.drawable);

而不是使用: layout.setBackground(getResources().getDrawable(R.drawable.img_wstat_tstorm));

而是使用:

layout.setBackground(ContextCompat.getDrawable(getActivity(), R.drawable.img_wstat_tstorm));

getActivity()在片段中使用,如果从活动调用使用this

于 2016-03-30T10:28:05.430 回答
-1
BitmapDrawable background = new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.mipmap.Nome_imgem));
        getSupportActionBar().setBackgroundDrawable(background);
于 2017-10-08T04:05:14.813 回答