getResources().getDrawable(int id) 替代

前段时间在开发过程中用到了getDrawable方法,但是发现该方法被屏蔽掉了,

替代方法

查看代码和在官网查询后发现为了适配5.1以上系统,官方新提供了这么一个方法

 public static final Drawable getDrawable(Context context, int id) {
        final int version = Build.VERSION.SDK_INT;
        if (version >= 21) {
            return ContextCompatApi21.getDrawable(context, id);
        } else {
            return context.getResources().getDrawable(id);
        }
    }

在源代码中可以看出来,为版本号21以上的设备提供了ContextCompatApi21.getDrawable(context, id);
这么一个方法,并且集合了之前的getDrawable方法

    本菜鸟遇到了这种问题,拿出来和诸位分享一下,希望各位能借鉴下

你可能感兴趣的:(代码迭代)