getColor(int id) deprecated on Android 6.0 Marshma

2018-12-31 21:16发布

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

@ColorInt
@Deprecated
public int getColor(@ColorRes int id) throws NotFoundException {
    return getColor(id, null);
}

What should I do?

17条回答
孤独总比滥情好
2楼-- · 2018-12-31 21:45

I don't want to include the Support library just for getColor, so I'm using something like

public static int getColorWrapper(Context context, int id) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        return context.getColor(id);
    } else {
        //noinspection deprecation
        return context.getResources().getColor(id);
    }
}

I guess the code should work just fine, and the deprecated getColor cannot disappear from API < 23.

查看更多
与风俱净
3楼-- · 2018-12-31 21:46

For Drawable

ContextCompat.getDrawable(getApplicationContext(),R.drawable.back_arrow)

For Color

 ContextCompat.getColor(getApplicationContext(),R.color.red)
查看更多
残风、尘缘若梦
4楼-- · 2018-12-31 21:46

You can use:

if (Build.VERSION.SDK_INT >= 23) {
    return ctx.getColor(R.color.YOURCOLOR);
} else {
    return ctx.getRecources().getColor(R.color.YOURCOLOR);
}

Tested on Android Marshmallow. It worked.

查看更多
看淡一切
5楼-- · 2018-12-31 21:50

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

查看更多
妖精总统
6楼-- · 2018-12-31 21:50

This way:

fab.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(this, R.color.badge_participants_counter_color)));
查看更多
余生请多指教
7楼-- · 2018-12-31 21:51

I got frustrated too. My need was very straightforward. All I wanted was the ARGB color from the resources, so I wrote a simple static method.

protected static int getARGBColor(Context c, int resId)
        throws Resources.NotFoundException {

    TypedValue color = new TypedValue();
    try {
        c.getResources().getValue(resId, color, true);
    }
    catch (Resources.NotFoundException e) {
        throw(new Resources.NotFoundException(
                  String.format("Failed to find color for resourse id 0x%08x",
                                resId)));
    }
    if (color.type != TYPE_INT_COLOR_ARGB8) {
        throw(new Resources.NotFoundException(
                  String.format(
                      "Resourse id 0x%08x is of type 0x%02d. Expected TYPE_INT_COLOR_ARGB8",
                      resId, color.type))
        );
    }
    return color.data;
}
查看更多
登录 后发表回答