不推荐使用getResources()。getColor()[重复]

本文翻译自:getResources().getColor() is deprecated [duplicate]

This question already has an answer here: 这个问题在这里已有答案:

Using: buildToolsVersion "22.0.1" , targetSdkVersion 22 in my gradle file. 使用: buildToolsVersion "22.0.1" ,我的gradle文件中的targetSdkVersion 22

I found that the useful getResources().getColor(R.color.color_name) is deprecated. 我发现不推荐使用有用的getResources().getColor(R.color.color_name)

What should I use instead? 我应该用什么呢?


#1楼

参考:https://stackoom.com/question/29boV/不推荐使用getResources-getColor-重复


#2楼

I found that the useful getResources().getColor(R.color.color_name) is deprecated. 我发现不推荐使用有用的getResources()。getColor(R.color.color_name)。

It is not deprecated in API Level 21, according to the documentation . 根据文档 ,它在API Level 21中不被弃用。

It is deprecated in the M Developer Preview. 在M开发者预览版弃用。 However, the replacement method (a two-parameter getColor() that takes the color resource ID and a Resources.Theme object) is only available in the M Developer Preview. 但是,替换方法(采用颜色资源ID和Resources.Theme对象的双参数getColor() )仅在M Developer Preview中可用。

Hence, right now, continue using the single-parameter getColor() method. 因此,现在,继续使用单参数getColor()方法。 Later this year, consider using the two-parameter getColor() method on Android M devices, falling back to the deprecated single-parameter getColor() method on older devices. 今年晚些时候,可以考虑使用这两个参数getColor()方法在Android M器件,回落至弃用单参数getColor()旧设备上的方法。


#3楼

It looks like the best approach is to use: 看起来最好的方法是使用:

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

eg: 例如:

yourView.setBackgroundColor(ContextCompat.getColor(applicationContext,
                            R.color.colorAccent))

This will choose the Marshmallow two parameter method or the pre-Marshmallow method appropriately. 这将适当地选择Marshmallow两参数方法或pre-Marshmallow方法。


#4楼

You need to use ContextCompat.getColor(), which is part of the Support V4 Library (so it will work for all the previous API). 您需要使用ContextCompat.getColor(),它是Support V4库的一部分(因此它适用于所有以前的API)。

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

As specified in the documentation, "Starting in M, the returned color will be styled for the specified Context's theme". 如文档中所述,“从M开始,返回的颜色将针对指定的Context主题设置样式”。 SO no need to worry about it. 所以不用担心。

You can add the Support V4 library by adding the following to the dependencies array inside your app build.gradle: 您可以通过将以下内容添加到app build.gradle中的dependencies数组来添加Support V4库:

compile 'com.android.support:support-v4:23.0.1'

#5楼

well it's deprecated in android M so you must make exception for android M and lower. 好吧,它在Android M中被弃用,所以你必须为android M和更低版本做例外。 Just add current theme on getColor function. 只需在getColor函数上添加当前主题即可。 You can get current theme with getTheme() . 您可以使用getTheme()获取当前主题。

This will do the trick in fragment, you can replace getActivity() with getBaseContext() , yourContext , etc which hold your current context 这将完成片段中的技巧,你可以用getBaseContext()yourContext等替换getActivity()来保存你当前的上下文

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    yourTitle.setTextColor(getActivity().getResources().getColor(android.R.color.white, getActivity().getTheme()));
}else {
    yourTitle.setTextColor(getActivity().getResources().getColor(android.R.color.white));
}

*ps : color is deprecated in M, but drawable is deprecated in L * ps:颜色在M中已弃用,但在L中不推荐使用drawable

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值