当前位置:网站首页>'setbackgrounddrawable() 'is deprecated, setbackgrounddrawable is obsolete

'setbackgrounddrawable() 'is deprecated, setbackgrounddrawable is obsolete

2022-06-10 12:59:00 yechaoa

setBackgroundDrawable() stay API 16(4.1) It's out of date

4.1 Then there are two ways to replace :

a、setBackgroundResource

b、setBackground

for example :

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

setBackgroundResource Method is called internally setBackground Method , and setBackground The internal call is still setBackgroundDrawable Method

setBackground Source code :

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

setBackgroundResource Source code :

@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);// Notice the call here 

        mBackgroundResource = resid;
    }
原网站

版权声明
本文为[yechaoa]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101133346315.html