首页 文章

在Android 6.0 Marshmallow(API 23)上弃用了getColor(int id)

提问于
浏览
631

Resources.getColor(int id) 方法已被弃用 .

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

我该怎么办?

17 回答

  • 20

    具有多API支持的代码:

    if (Build.VERSION.SDK_INT < 23) {
        view.setBackgroundColor(getResources().getColor(android.R.color.white));
    } else {
        view.setBackgroundColor(getContext().getColor(android.R.color.white));
    }
    
  • 2

    从Android支持库23开始,
    ContextCompat 添加了一个新的getColor()方法 .

    它来自官方JavaDoc的描述:

    返回与特定资源ID关联的颜色从M开始,将为指定的Context主题设置返回的颜色样式 .

    那么, just call

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

    你可以查看 ContextCompat.getColor() source code on GitHub .

  • 28

    TL;博士:

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

    说明:

    您将需要使用ContextCompat.getColor(),它是Support V4库的一部分(它适用于所有以前的API) .

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

    如果您还没有使用支持库,则需要将以下行添加到应用程序 build.gradle 中的 dependencies 数组中(注意:如果您已经使用了appcompat(V7)库,则它是可选的):

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

    如果您关心主题,则文档指定:

    从M开始,返回的颜色将针对指定的Context主题设置样式

  • 2

    我不想仅为getColor包含支持库,所以我使用的是类似的东西

    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);
        }
    }
    

    我想代码应该可以正常工作,不推荐的 getColor 不能从API <23消失 .

  • 1

    在Android Marshmallow中,许多方法都已弃用 .

    例如,获得颜色使用

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

    也是为了获得可绘制的用途

    ContextCompat.getDrawable(context, R.drawable.drawble_name);
    
  • 0

    对于那里的所有Kotlin用户:

    context?.let {
        val color = ContextCompat.getColor(it, R.color.colorPrimary)
        // ...
    }
    
  • 462

    对于Drawable

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

    对于颜色

    ContextCompat.getColor(getApplicationContext(),R.color.red)
    
  • 38

    您可以使用:

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

    在Android Marshmallow上测试过 . 有效 .

  • 8

    最简短的答案是:

    ContextCompat.getColor(context, R.color.colorAccent);
    
  • 2

    在API 23中不推荐使用getColor()方法,它是在ContextCompat中引入的 . 在使用ContextCompat使用getColor()方法之前,在build.gradle脚本中添加依赖项,即:

    dependencies{
    
        // Other stuff
    
        compile 'com.android.support:support-v4:23.0.0'
    }
    

    然后可以使用getColor()方法,如:

    int colorcode = ContextCompat.getColor(getApplicationContext(), R.color.your_color);
    
  • 1237

    使用Android支持库中 ResourcesCompatgetColor(Resources, int, Theme) 方法 .

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

    我认为这比 ContextCompatgetColor(Context, int) 更好地反映了你的问题,因为你问的是 Resources . 在API级别23之前,将不会应用主题,并且方法将调用 getColor(int) ,但您不会使用已弃用的警告 . 主题也可能是 null .

  • 17

    在Mono / xamarin.android中我遇到了同样的问题,以下代码完美地运行:

    ContextCompat.GetColor (context, Resource.Color.myColor)
    

    在我的情况下,上下文是我的活动(Android.Support.V7.App.AppCompatActivity)所以我的实际用例是:

    var color = new Android.Graphics.Color(ContextCompat.GetColor(this, Resource.Color.myColor))
    
  • 1

    用这个:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        return context.getResources().getColor(id, context.getTheme());
    } else {
        return context.getResources().getColor(id);
    }
    
  • 7

    即使_285907_在棉花糖中被弃用,也就是替换方法
    getColor(int, Theme) 可用 . 哪个可以作为 ContextCompat.getColor(context, R.color.your_color); 的替代品

    有关更多信息,请参阅this .

  • 7

    如果您不一定需要资源,请使用parseColor(String)
    Color.parseColor("#cc0066")

  • 0

    这条路:

    fab.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(this, R.color.badge_participants_counter_color)));
    
  • 4

    我也很沮丧 . 我的需要非常简单 . 我想要的只是来自资源的ARGB颜色,所以我写了一个简单的静态方法 .

    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;
    }
    

相关问题