'setBackgroundDrawable()' is deprecated,setBackgroundDrawable过时

简介: 'setBackgroundDrawable()' is deprecated,setBackgroundDrawable过时

setBackgroundDrawable()在API 16(4.1)已经过时了

image.png

4.1之后有两种方法可以代替:


a、setBackgroundResource

image.png

b、setBackground

image.png

例如:

textView.setBackgroundResource(R.drawable.icon);
textView.setBackground(ContextCompat.getDrawable(this, R.drawable.icon));


setBackgroundResource方法在内部还是调用的setBackground方法,而 setBackground内部调用的还是setBackgroundDrawable方法


setBackground源码:


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


setBackgroundResource源码:


@RemotableViewMethod
    public void setBackgroundResource(@DrawableRes int resid) {
        if (resid != 0 && resid == mBackgroundResource) {
            return;
        }
        Drawable d = null;
        if (resid != 0) {
            d = mContext.getDrawable(resid);
        }
        setBackground(d);//注意这里的调用
        mBackgroundResource = resid;
    }

目录
相关文章
|
2月前
|
安全 编译器 Go
【Golang】如何声明弃用(Deprecated)
【Golang】如何声明弃用(Deprecated)
423 5
|
2月前
|
iOS开发
‘authorizationStatus‘ is deprecated: first deprecated in iOS 14.0
‘authorizationStatus‘ is deprecated: first deprecated in iOS 14.0
25 0
|
小程序 JavaScript API
Unparenthesized `a ? b : c ? d : e` is deprecated. Use either `(a ? b : c) ? d : e` or `a ? b : (c ?
Unparenthesized `a ? b : c ? d : e` is deprecated. Use either `(a ? b : c) ? d : e` or `a ? b : (c ?
735 0
|
8月前
|
Java
jpa中PageRequest @Deprecated和Sort @Deprecated的最新使用方法
jpa中PageRequest @Deprecated和Sort @Deprecated的最新使用方法
|
9月前
|
安全 API
什么是软件开发领域的 obsolete 或者 deprecated 含义
什么是软件开发领域的 obsolete 或者 deprecated 含义
|
11月前
|
IDE 安全 Java
使用 kotlin.Deprecated,优雅废弃你的过时代码
使用 kotlin.Deprecated,优雅废弃你的过时代码
218 0
|
API Python
全网首发:warning: #warning “Using deprecated NumPy API, disable it by “ “#defining NPY_NO_DEPRECATED_API
全网首发:warning: #warning “Using deprecated NumPy API, disable it by “ “#defining NPY_NO_DEPRECATED_API
217 0
|
API
'getColor(int)' is deprecated ,getColor过时
'getColor(int)' is deprecated ,getColor过时
235 0
'getColor(int)' is deprecated ,getColor过时
mTabLayout.setOnTabSelectedListener is deprecated
mTabLayout.setOnTabSelectedListener is deprecated
115 0
mTabLayout.setOnTabSelectedListener is deprecated
|
API
'getWidth()' is deprecated,'getHeight()' is deprecated
'getWidth()' is deprecated,'getHeight()' is deprecated
116 0
'getWidth()' is deprecated,'getHeight()' is deprecated