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

前端 未结 11 1969
我寻月下人不归
我寻月下人不归 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:10

    tl;dr:

    ContextCompat.getColor(context, R.color.my_color)
    

    Explanation:

    You will need to use ContextCompat.getColor(), which is part of the Support V4 Library (it will work for all the previous APIs).

    ContextCompat.getColor(context, R.color.my_color)
    

    If you don't already use the Support Library, you will need to add the following line to the dependencies array inside your app build.gradle (note: it's optional if you already use the appcompat (V7) library):

    compile 'com.android.support:support-v4:23.0.0' # or any version above
    

    If you care about themes, the documentation specifies that:

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

提交回复
热议问题