Android中getDrawable和getColor过时的替

2019-03-29  本文已影响0人  闹闹也会有脾气

前言

Android SDK 升级到 23 之后,getDrawable和getColor方法提示过时。

解决方案

getResources().getColor 替换成 ContextCompat.getColor getResources().getDrawable 替换成 ContextCompat.getDrawable

例子如下:
int color = getResources().getColor(R.color.colorAccent);
替换为
int color2 = ContextCompat.getColor(this, R.color.colorAccent);

Drawable drawable = getResources().getDrawable(R.mipmap.ic_launcher);
替换为
Drawable drawable2 = ContextCompat.getDrawable(this,R.mipmap.ic_launcher);
上一篇 下一篇

猜你喜欢

热点阅读