我正在尝试使用在res/values/colors.xml
文件中创建的颜色。但是,当我动态进行调用时,程序不会编译,而是建议: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>
Activity 代码:
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);
最佳答案
将代码更改为以下内容:
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));
关于android - 如何在Android中动态调用资源中的颜色?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12374146/