如果您通過以下的代碼來獲取定義的顏色值
context.getResources().getColor(R.color.some_color_resource_id);
在 Android Studio 中會有一個 lint 警告,提示您 Resources#getColor(int) 在 Marshmallow 中被廢棄了,建議使用主題可知的 Resources#getColor(int, Theme) 函數。 為了避免該警告,則可以使用 ContextCompat:
ContextCompat.getColor(context, R.color.some_color_resource_id);
該函數的實現是這樣的:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { return context.getResources().getColor(id, context.getTheme());} else { return context.getResources().getColor(id);}看起來很簡單。但是為什么會這樣呢? 為什么會開始使用帶主題的函數而廢棄之前的函數呢?
Resources#getColor(int) & Resources#getColorStateList(int) 的問題
首先來看看這兩個被廢棄的函數是干啥的:
主站蜘蛛池模板:
漯河市|
拜城县|
平顶山市|
溧水县|
拜城县|
平定县|
彝良县|
利津县|
宁乡县|
迁安市|
棋牌|
突泉县|
维西|
团风县|
临朐县|
鸡东县|
毕节市|
邯郸市|
邯郸县|
太湖县|
大足县|
越西县|
祥云县|
平果县|
普兰县|
门头沟区|
吐鲁番市|
阿克苏市|
绥宁县|
泰和县|
临猗县|
平塘县|
齐齐哈尔市|
来凤县|
南皮县|
无棣县|
镇康县|
政和县|
屯昌县|
宁南县|
德保县|