今天遇见一个问题. 在studio2.3编译时,colorRes报红,
有错误的代码,如下
@SuppressWarnings("deprecation") public void setBackgroundColor(int colorRes) { if (getBackground() instanceof ShapeDrawable) { final Resources res = getResources(); ((ShapeDrawable) getBackground()).getPaint().setColor(res.getColor(colorRes)); } }
解决方案 如下:
在getColor源码中,取出@ColorRes.赋值给构造函数的值
@ColorInt @Deprecated public int getColor(@ColorRes int id) throws NotFoundException { return getColor(id, null); }
public void setBackgroundColor(@ColorRes int colorRes) { if (getBackground() instanceof ShapeDrawable) { ((ShapeDrawable) getBackground()).getPaint().setColor(getResources().getColor(colorRes)); } }