美文网首页
Android中getDrawable和getColor过时的替

Android中getDrawable和getColor过时的替

作者: 闹闹也会有脾气 | 来源:发表于2019-03-29 16:07 被阅读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);

相关文章

网友评论

      本文标题:Android中getDrawable和getColor过时的替

      本文链接:https://www.haomeiwen.com/subject/hbgzqxtx.html