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

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

    Use the getColor(Resources, int, Theme) method of the ResourcesCompat from the Android Support Library.

    int white = new ResourcesCompat().getColor(getResources(), R.color.white, null);
    

    I think it reflect better your question than the getColor(Context, int) of the ContextCompat since you ask about Resources. Prior to API level 23, the theme will not be applied and the method calls through to getColor(int) but you'll not have the deprecated warning. The theme also may be null.

提交回复
热议问题