如何让fgcolor属性在最新的Android版本上运行?

时间:2013-09-11 07:38:01

标签: android android-resources

我曾经能够这样做:

<string name="foo">white <font fgcolor="#ff6890a5">blue</font></string>

但现在它不再起作用了。它似乎是整数解析代码中的一个错误;见https://code.google.com/p/android/issues/detail?id=58192

问题是,我现在收到客户投诉; ;我等不及修复错误。

是否有人知道解决方法,例如使用color.xml中的命名资源或类似的东西?

ETA:我发现fgcolor="blue"仍然可以正常工作,但这是错误的蓝色阴影。某处是否有合法颜色名称列表?也许我能找到一个足够接近的人。如果颜色是没有设置高位的数字,它也可以工作,如#7f6890a5,但当然这太微弱而无用;我需要纯色,而不是半透明。

ETA:浏览源代码显示以下颜色:

  aqua      0x00FFFF
  black     0x000000
  blue      0x0000FF
  fuchsia   0xFF00FF
  green     0x008000
  grey      0x808080
  lime      0x00FF00
  maroon    0x800000
  navy      0x000080
  olive     0x808000
  purple    0x800080
  red       0xFF0000
  silver    0xC0C0C0
  teal      0x008080
  white     0xFFFFFF
  yellow    0xFFFF00

这不能解决我的问题,但也许其他搜索此问题的人可能会发现这些信息很有用。

3 个答案:

答案 0 :(得分:9)

我们如何利用没有高位设置的颜色仍然有效并且只用正确的颜色替换它的事实?所以你可以有这样的方法:

private CharSequence fixSpanColor(CharSequence text) {
    if (text instanceof Spanned) {
        final SpannableString s = new SpannableString(text);
        final ForegroundColorSpan[] spans = s.getSpans(0, s.length(), ForegroundColorSpan.class);
        for (final ForegroundColorSpan oldSpan : spans) {
            final ForegroundColorSpan newSpan = new ForegroundColorSpan(oldSpan.getForegroundColor() | 0xFF000000);
            s.setSpan(newSpan, s.getSpanStart(oldSpan), s.getSpanEnd(oldSpan), s.getSpanFlags(oldSpan));
            s.removeSpan(oldSpan);
        }
        return s;
    } else {
        return text;
    }
}

然后,您需要通过此方法传递具有所需颜色重音的任何文本,最简单的示例是修改所有调用,如下所示:

tv.setText(getText(R.string.foo));

要:

tv.setText(fixSpanColor(getText(R.string.foo)));

希望根据您的代码的结构,可能已经有一个中心位置可以添加这个额外的方法调用。

答案 1 :(得分:3)

  

我的客户端代码中有一些可怕的解决方法来手动重置   ForegroundColor调到适当的颜色,但不是很好   必须这样做。

我认为问题记者谈论的解决方法如下:

将字符串定义为:

<string name="foo">white blue</string>

在您的活动中:

TextView tv = (TextView) findViewById(R.id.textView1);

SpannableString spannableString = new 
                       SpannableString(getResources().getString(R.string.foo));

ForegroundColorSpan fcs = new 
                       ForegroundColorSpan(getResources().getColor(R.color.bluish));

spannableString.setSpan(fcs, spannableString.toString().indexOf(" ") + 1,
                       spannableString.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);

tv.setText(spannableString);

R.color.bluish定义为<color name="bluish">#ff6890a5</color>

但是,使用“”(空格)来区分和应用ForegroundColorSpan只有在定义了少量字符串时才有用。


以下修改实际上可能更容易执行:

将字符串定义为:

<string name="foo_sep_1"><![CDATA[white <font color=\"#6890a5\">blue</font>]]></string>

或者:

<string name="foo_sep_1">white &lt;font color="#6890a5"&gt;blue&lt;/font&gt;</string>

在您的活动中:

TextView tv2 = (TextView) findViewById(R.id.textView2);

tv2.setText(Html.fromHtml(getResources().getString(R.string.foo_sep_1)));

请注意颜色代码:HTML颜色代码没有alpha个值(RRGGBB将起作用,AARRGGBB不起作用)


另一种解决方法是直接使用Html.fromHtml(String)

TextView tv3 = (TextView) findViewById(R.id.textView3);

tv3.setText(Html.fromHtml("white <font color='#6890a5'>blue</font>"));

答案 2 :(得分:0)

感谢TWiStErRob找到了一个不涉及https://stackoverflow.com/a/11577658/338479代码的解决方案。引用:

  

对于7fffffff以上的任何颜色应用以下内容:&lt; font   颜色= “#ff6890a5” &GT;将ff6890a5放入计算器(可选择转换   首先是十进制)并翻转符号,然后(可选地转换回   hexa)取最后8个十六进制数字并使用&lt; font   颜色= “ - #00976F5B” &GT;