Android:用户界面:线性梯度不与自定义颜色工作



我正在尝试使用Shader和LinearGradient在我的TextView中添加线性梯度。只有当我使用像Color这样的标准颜色时,这才有效。LTGRAY,颜色。红色等。如果我使用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需要先解析颜色资源

最新更新