学习《疯狂android》其中涉及到给组件设置字体颜色的代码。下面的集中方法和报错。最后在一个英文网站上找到了最后一种方法,不会提是错误
/*
第一种:Should pass resolved color instead of resource id here: getResources().getColor(R.color.red)
*/
//text.setTextColor(R.color.red);
/*
The method getColor(int) from the type Resources is deprecated
*/
//text.setTextColor(getResources().getColor(R.color.red));
/*
在网上查询都说这个方法是替换已经过时的方法:getResources().getColor(R.color.red)的。但是不好使,也无法在API上查询到ContextCompat下有getColor()方法。也可能是我不会用
The method getColor(TextView, int) is undefined for the type ContextCompat
*/
//ContextCompat.getColor(text,R.color.red);
/*
这个用法同样会提是上面的错误
*/
//text.setTextColor(ContextCompat.getColor(text, R.color.red));
/*
在一个英文网站上查询到,有个人说他用下面这个方法设置字体颜色。我使用后至少还没有报错,不知道是否有效果
*/
text.setTextColor(Color.RED);