何时使用ContextCompat类 [英] When to use ContextCompat class

查看:95
本文介绍了何时使用ContextCompat类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道何时在应用程序中使用 ContextCompact 类.基本上,它的用途是什么?何时使用?我已经阅读过开发人员网站,它说 ContextCompact 是访问Context中的功能的帮助者".但是这行是什么意思?

I want to know when to use ContextCompact class in an application. Basically what is it used for and when to use it? I have read developers site, it says ContextCompact is a "helper for accessing features in Context". But what does this line mean?

推荐答案

ContextCompat 是用于用基本上下文替换某些工作的类.

ContextCompat is a class for replacing some work with base context.

例如,如果您之前使用过类似的东西

For example if you used before something like

getContext().getColor(R.color.black);

自android 6.0(API 22+)起,其已弃用,因此您应使用:

Now its deprecated since android 6.0 (API 22+) so you should use:

getContext().getColor(R.color.black,theme);

或使用 ContextCompat 填充主题会自动取决于您的 Context 主题:

or use ContextCompat which fill theme automatically depends on your Context's theme:

ContextCompat.getColor(getContext(),R.color.black)

getDrawable

ContextCompat 还包含其他用于API 22+功能的方法,例如检查权限或向堆栈中添加多个活动

Also ContextCompat contains other methods for functional of API 22+ such as checking permissions or adding multiple activity to stack

这篇关于何时使用ContextCompat类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆