getColor(int id) deprecated on Android 6.0 Marshmallow (API 23)

前端 未结 11 1839
我寻月下人不归
我寻月下人不归 2020-11-22 12:29

The Resources.getColor(int id) method has been deprecated.

@ColorInt
@Deprecated
public int getColor(@ColorRes int id) throws NotFoundException          


        
11条回答
  •  长发绾君心
    2020-11-22 13:19

    Starting from Android Support Library 23,
    a new getColor() method has been added to ContextCompat.

    Its description from the official JavaDoc:

    Returns a color associated with a particular resource ID

    Starting in M, the returned color will be styled for the specified Context's theme.


    So, just call:

    ContextCompat.getColor(context, R.color.your_color);
    

    You can check the ContextCompat.getColor() source code on GitHub.

提交回复
热议问题