Android setBackgroundDrawable()已弃用

Android setBackgroundDrawable()已弃用,android,deprecated,deprecation-warning,Android,Deprecated,Deprecation Warning,所以我的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.draw

所以我的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));
}
intsdk=android.os.Build.VERSION.sdk\u int;
if(sdk
但是,我在“setBackground()”中得到了一个错误


那么,你将如何处理它呢?

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


有关这一切的更多信息,请参见

由于getResources().getDrawable()将id(int)而不是drawable作为其参数,因此出现错误。试试这个:


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

这很有趣,因为该方法已被弃用,但如果您查看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);
    }

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

setBackgroundResource(R.drawable.img_wstat_tstorm);

我使用的是Minsdk版本16和targetSdkVersion 23 以下内容适用于我,它使用 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()
在片段中使用,如果从活动调用,则使用

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

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

自2018年8月15日起更正

使用支持库

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

自2018年11月23日起更正

科特林:

view.background=resources.getDrawable(R.drawable.ic_图像,主题)
如果包含主题参数。

请使用以下选项:

myView.background = ContextCompat.getDrawable(context, R.id.my_drawable)

您是否收到错误或警告?清单中的min sdk版本值是多少?请使用setbackgroundresource(R.drawable.img_wstat_tstorm);对于更高版本。setBackgroundDrawable在更高版本中被删除,此希望帮助youMin sdk是15。我有红色下划线的“setBackground()”,但应用程序正在运行,所以我猜这是一个警告。您必须获得Add@superswarningsetbackground,只需要可绘制的Id。您不正确。来自API文档:android.view.view.setBackground(可绘制背景);参数:背景可作为背景使用,或null可删除背景。请注意,
setBackground()
不适用于API16之前版本,另一种选择可能是
setBackgroundResource
minSdk 15提问如果你能用几句话概括一下你在这里要做的事情,那真的会很有帮助。。。
//Java
view.setBackground(ActivityCompat.getDrawable(context, R.drawable.bg))

//Kotlin 
view.background = ActivityCompat.getDrawable(context, R.drawable.bg)
Drawable drawable = ResourcesCompat.getDrawable(getResources(), drawableRes, null);
ViewCompat.setBackground(layout, drawable);
myView.background = ContextCompat.getDrawable(context, R.id.my_drawable)