setBackgroundDrawable()在API 16(4.1)已经过时了
4.1之后有两种方法可以代替:
a、setBackgroundResource
b、setBackground
例如:
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; }