@act262
2017-09-19T04:45:05.000000Z
字数 716
阅读 1135
compat
正确使用 support 库
minSdk<16
时必须用setBackgroundDrawable
设置背景,
public class ViewCompat {
public static void setBackground(View targetView, Drawable drawable) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
targetView.setBackground(drawable);
} else {
//noinspection deprecation
targetView.setBackgroundDrawable(drawable);
}
}
}
在Support library 25+以后ViewCompat
自带了setBackground
方法
ViewCompat.setBackground(mView, background);
ContextCompat#getColorStateList
替换
public static final ColorStateList getColorStateList(Context context, @ColorRes int id)
Contextpat#getDrawable
替换
public static final Drawable getDrawable(Context context, @DrawableRes int id)