1. 程式人生 > >好用的 Android getDrawable過時的替代方法

好用的 Android getDrawable過時的替代方法

之前程式碼中獲取Drawable都是使用如下方法:

Drawable TopDrawableOne = getResources().getDrawable(R.drawable.icon_test);

但是此方法過時,不推薦使用,那麼肯定是有其他替代方法的,經過查詢,很多網友都是推薦使用

Drawable getDrawable(int id, Resources.Theme theme), 第二個引數@theme可以為空值.或Context.getDrawable(int)

但是感覺此方法不是很好用就在此查詢有沒有其他好用的方法,最後找到

 Drawable TopDrawableOne = ContextCompat.getDrawable(context,R.drawable.icon_test);

這種方式,看著舒服,也是谷歌推薦使用的。