丑話說前頭敛劝,本文轉(zhuǎn)自:
http://stackoverflow.com/questions/29041027/android-getresources-getdrawable-deprecated-api-22
我們在項目經(jīng)常會用到從資源文件中獲取drawable對象的時候培廓,但好像最新的api已經(jīng)棄用
getResources().getDrawable(R.drawable.ic_launcher);
我們可以這樣做:
ResourcesCompat.getDrawable(getResources(), R.drawable.ic_launcher, null);
以上這種模式是沒有指定主題(theme)的躺涝,要指定主題就直接把最后一個參數(shù)傳一個主題進(jìn)去
也就是這樣:ResourcesCompat.getDrawable(getResources(),R.drawable.name,anotherTheme);
附:
還可以這樣
ContextCompat.getDrawable(getActivity(),R.drawable.name);