setBackgroundDrawable()已过时。

107
我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()”处遇到了一个错误。

那么,你会如何处理它?


你收到了错误还是警告? - Bryan Herbst
在清单文件中,你的min sdk版本是多少? - Manmohan Badaya
4
请使用 setbackgroundresource(R.drawable.img_wstat_tstorm); 替代 setBackgroundDrawable,因为在较高版本中 setBackgroundDrawable 已经过时。希望这可以帮助您。 - prakash
最低sdk为15。我在“setBackground()”下面加了下划线,但应用程序可以运行,所以我猜这只是一个警告。 - Makoto
您必须收到Add @SupressWarning - SweetWisher ツ
11个回答

118

这是一个有趣的话题。显然,您现在的做法是正确的。实际上,这只是一种命名决策的更改。正如这个答案所指出的那样,setBackground() 只是调用了 setBackgroundDrawable()

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

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

你可以查看 这个帖子 获取更多关于所有内容的信息。

21
请注意,“setBackground()”在API16之前不能使用,可以选择“setBackgroundResource”作为替代方案。 - Mood

31

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

setBackgroundResource(R.drawable.img_wstat_tstorm);

无法解析方法“setBackgroundResource”。应该使用“setBackgroundDrawableResource”。 - Darksymphony

20

截至2018年8月15日的正确信息。

使用支持库。

Drawable drawable = ResourcesCompat.getDrawable(getResources(), drawableRes, null);
ViewCompat.setBackground(layout, drawable);

被低估的答案,如金子般珍贵。 - Ezequiel Adrian

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

8
您遇到错误是因为getResources().getDrawable()需要一个id(int)而不是drawable作为其参数。请尝试以下方法: layout.setBackground(getResources().getDrawable(R.id.img_wstat_tstorm));

setBackground 只接受 Drawable Id。 - SweetWisher ツ
你是错误的。来自API文档:android.view.View.setBackground(Drawable background);参数:background 用作背景的Drawable,或null以删除背景。 - David C Adams

5
使用此代码:
myView.background = ContextCompat.getDrawable(context, R.id.my_drawable)

4
//Java
view.setBackground(ActivityCompat.getDrawable(context, R.drawable.bg))

//Kotlin 
view.background = ActivityCompat.getDrawable(context, R.drawable.bg)

2

在我的情况下,这是正确的。 解决此问题。

 imageView.setBackgroundResource(images[productItem.getPosition()]);

1

截至2018年11月23日

Kotlin:

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

如果您包含了Theme参数。

-1
BitmapDrawable background = new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.mipmap.Nome_imgem));
        getSupportActionBar().setBackgroundDrawable(background);

如果您能用简短的话概括一下您在这里尝试做什么,那将非常有帮助... - arkascha

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接