日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

android 解决getColor()方法过时

發(fā)布時(shí)間:2024/4/15 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 解决getColor()方法过时 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.



今天,簡(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)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。