如何在Android中dynamic调用资源中的颜色?

我正在尝试使用res/values/colors.xml文件中创build的颜色。 但是,当我dynamic调用该程序不编译,而是build议: Should pass resolved color instead of a resource id.

色标:

 <?xml version="1.0" encoding="utf-8"?> <resources> <color name="gold">#857500</color> <color name="green">#0B3B0B</color> </resources> 

活动代码:

 title.setTextColor(R.color.green); help.setTextColor(R.color.gold); settings.setTextColor(R.color.gold); logout.setTextColor(R.color.gold); profile.setTextColor(R.color.green); 

Solutions Collecting From Web of "如何在Android中dynamic调用资源中的颜色?"

将代码更改为以下内容:

 title.setTextColor(getResources().getColor(R.color.green)); help.setTextColor(getResources().getColor(R.color.gold)); settings.setTextColor(getResources().getColor(R.color.gold)); logout.setTextColor(getResources().getColor(R.color.gold)); profile.setTextColor(getResources().getColor(R.color.green));