android 解决getColor()方法过时
今天,簡(jiǎn)單講講android里如何解決getColor()方法過(guò)時(shí)的問(wèn)題。
之前,我寫博客講了程序員需要解決過(guò)時(shí)的方法的問(wèn)題,Google會(huì)提供過(guò)時(shí)函數(shù)的替代函數(shù),程序員有責(zé)任找到替代函數(shù),并且解決過(guò)時(shí)的函數(shù)。所以,我檢測(cè)代碼時(shí)發(fā)現(xiàn)getColor()方法已經(jīng)過(guò)時(shí),自己在網(wǎng)上查找了資料,找到了替代函數(shù),解決了問(wèn)題。
getColor()過(guò)時(shí)過(guò)時(shí)的源碼:
/*** Returns a color integer associated with a particular resource ID. If the* resource holds a complex {@link ColorStateList}, then the default color* from the set is returned.** @param id The desired resource identifier, as generated by the aapt* tool. This integer encodes the package, type, and resource* entry. The value 0 is an invalid identifier.** @throws NotFoundException Throws NotFoundException if the given ID does* not exist.** @return A single color value in the form 0xAARRGGBB.* @deprecated Use {@link #getColor(int, Theme)} instead.*/@ColorInt@Deprecatedpublic int getColor(@ColorRes int id) throws NotFoundException {return getColor(id, null);}
新替代getColor()的源碼:
/*** Returns a color associated with a particular resource ID* <p>* Starting in {@link android.os.Build.VERSION_CODES#M}, the returned* color will be styled for the specified Context's theme.** @param id The desired resource identifier, as generated by the aapt* tool. This integer encodes the package, type, and resource* entry. The value 0 is an invalid identifier.* @return A single color value in the form 0xAARRGGBB.* @throws android.content.res.Resources.NotFoundException if the given ID* does not exist.*/@ColorIntpublic static final int getColor(Context context, @ColorRes int id) {final int version = Build.VERSION.SDK_INT;if (version >= 23) {return ContextCompatApi23.getColor(context, id);} else {return context.getResources().getColor(id);}}在新的方法中進(jìn)行了判斷,進(jìn)行6.0系統(tǒng)的區(qū)分,針對(duì)于6.0以下還是調(diào)用了原來(lái)的getColor方法,對(duì)于6.0+的使用了新的方法進(jìn)行替代,這個(gè)就不用說(shuō)了吧,一般的升級(jí)都會(huì)對(duì)老版本進(jìn)行兼容,具體的使用方法也稍有變化
過(guò)時(shí)getColor()方法使用:
過(guò)時(shí)getColor()方法使用:
新的getColor()方法使用:
這里還提一下getDrawable過(guò)時(shí)的替代方法,基本是一樣的。
用getDrawable()方法過(guò)時(shí)了
現(xiàn)象
網(wǎng)友推薦
谷歌查詢結(jié)果
android 解決getColor()方法過(guò)時(shí)就講完了。
就這么簡(jiǎn)單。
總結(jié)
以上是生活随笔為你收集整理的android 解决getColor()方法过时的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Android RelativeLayo
- 下一篇: android 使用Photoshop获