5

我正在尝试使用 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>
4

1 回答 1

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);

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

于 2013-03-13T13:43:19.863 回答