我正在尝试使用 Shader 和 LinearGradient 向我的 TextView 中的文本添加线性渐变。仅当我使用 Color.LTGRAY、Color.MAGENTA 等标准颜色时才有效。如果我使用 custom_colors.xml 中的颜色,则不会显示渐变。有什么想法可以让我使用自定义颜色吗?
Shader txtShad=new LinearGradient(0, 0, 0, 10, new int[]{R.color.el_amethyst,R.color.el_maroon}, null, TileMode.MIRROR);
textview_dummy.getPaint().setShader(txtShad);
下面是 custom_colors.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="el_maroon">#CD2990</color>
<color name="el_amethyst">#9D6B84</color>
</resources>
最佳答案
试试这个:
Shader txtShad = new LinearGradient(0, 0, 0, 10, new int[] { getResources().getColor(R.color.el_amethyst), getResources().getColor(R.color.el_maroon) }, null, TileMode.MIRROR);
TL;DR 你需要先解析颜色资源
关于Android:用户界面:LinearGradient 不适用于自定义颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15387004/