最新消息:VPS服务器又从fzhost.net换回Linode了,主题仍用朋友推荐的大前端D8

【已解决】Android中使用setBackgroundDrawable错误提示:The method setBackgroundDrawable(Drawable) from the type View is deprecated

Android crifan 291浏览 0评论

【问题】

如下android代码:

   Drawable statusQuestionDrawable = resources.getDrawable(R.drawable.status_question);
   statusView.setBackgroundDrawable(statusQuestionDrawable);

结果提示:

The method setBackgroundDrawable(Drawable) from the type View is deprecated

【解决过程】

1.很明显是该函数被废弃了。

但是换成啥,目前不知道。

2.参考:

The method setBackgroundDrawable(Drawable) from the type View is deprecated求解-CSDN论坛-CSDN.NET-中国最大的IT技术社区

改为:

statusView.setBackground(resources.getDrawable(R.drawable.status_question));

结果却又提示:

Call requires API level 16 (current min is 14): android.widget.TextView#setBackground

3.再改为:

statusView.setBackgroundResource(R.drawable.status_question);

就可以了:

change from setBackgroundDrawable to setBackgroundResource

4.后来看到:

eclipse – Android – set layout background programmatically – Stack Overflow

android – Deprecated method, but replacing method requires higher api – Stack Overflow

其中解释的更清楚。

 

【总结】

当出现:

The method setBackgroundDrawable(Drawable) from the type View is deprecated

时,把:

setBackgroundDrawable

换为

setBackgroundResource

即可。

且传入的参数直接是resource的id,无需再去通过ID获得View,更加方便。

 

附上对应的API的解释:

void android.view.View.setBackgroundResource(int resid)

public void setBackgroundResource (int resid)

Added in API level 1

Set the background to a given resource. The resource should refer to a Drawable object or 0 to remove the background.

Related XML Attributes
Parameters

resid

The identifier of the resource.

供参考。

转载请注明:在路上 » 【已解决】Android中使用setBackgroundDrawable错误提示:The method setBackgroundDrawable(Drawable) from the type View is deprecated

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
13 queries in 0.203 seconds, using 9.99MB memory