Android:用户界面:LinearGradient 不适用于自定义颜色

Ank*_*kit 5 user-interface android linear-gradients textview

我正在尝试使用 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);
Run Code Online (Sandbox Code Playgroud)

下面是custom_colors.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <color name="el_maroon">#CD2990</color>
  <color name="el_amethyst">#9D6B84</color>
</resources>
Run Code Online (Sandbox Code Playgroud)

Ahm*_*mad 5

尝试这个:

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);
Run Code Online (Sandbox Code Playgroud)

TL;DR 您需要首先解析颜色资源