【问题标题】:Android: User Interface: LinearGradient not working with custom colorsAndroid:用户界面:LinearGradient 不使用自定义颜色
【发布时间】:2013-03-13 13:40:44
【问题描述】:

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

【问题讨论】:

    标签: android user-interface textview linear-gradients


    【解决方案1】:

    试试这个:

    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 你需要先解析颜色资源

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-06
      • 2012-09-01
      • 2021-01-29
      • 2010-09-05
      • 2020-10-05
      相关资源
      最近更新 更多