我怎样才能获得fgcolor属性上班近的Andr​​oid版本? [英] How can I get the fgcolor attribute to work on recent Android versions?

查看:151
本文介绍了我怎样才能获得fgcolor属性上班近的Andr​​oid版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经是能够做到这一点:

I used to be able to do this:

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

但现在行不通了。这似乎是在整数解析code错误;看<一href="https://$c$c.google.com/p/android/issues/detail?id=58192">https://$c$c.google.com/p/android/issues/detail?id=58192

但问题是,我收到客户投诉的现在的;我不能等待的错误是固定的。

Problem is, I'm getting customer complaints now; I can't wait for the bug to be fixed.

是否有人知道一个变通办法,如利用color.xml或东西命名资源类的?

Does anybody know a work-around, such as using named resources from color.xml or something like that?

ETA:我发现 fgcolor =蓝色仍然有效,但它是蓝色的错误的阴影。是否有合法的颜色名某处的名单?也许我可以找到一个足够接近。它也可以,如果颜色是一个数字,没有高位集,如#7f6890a5 ,当然这是太微弱是有益的;我需要一个纯色,没有半透明。

ETA: I've discovered fgcolor="blue" still works, but it's the wrong shade of blue. Is there a list of legal color names somewhere? Maybe I could find one that's close enough. It also works if the color is a number without the high bit set, like #7f6890a5, but of course that's too faint to be useful; I need a solid color, not semi-transparent.

ETA:浏览源$ C ​​$ C显示这些颜色:

ETA: browsing source code shows these colors:

  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

这并不解决我的问题,但也许其他人寻找在这个问题上能找到这些信息很有用。

This doesn't fix my problem, but perhaps other people searching on this question could find this information useful.

推荐答案

怎么样,我们利用这些颜色没有高位设置仍然有效,只是用正确的颜色替换它的事实?所以,你可以有一个方法是这样的:

How about we leverage the fact that colors without the high bit set still works and just replace it with the correct colors? So you can have a method like this:

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

您接着需要通过这种方法来传递与所需的彩色口音的任何文字,最简单的例子是修改这样所有来电:

You will then need to pass any text with the required color accent through this method, the simplest example would be modifying all calls like this:

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

要:

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

希望,这取决于你的code的结构,有可能已经是中央的地方,你可以添加这个额外的方法调用。

Hopefully, depending on how your code is structured, there might already a central place where you can add this extra method call.

这篇关于我怎样才能获得fgcolor属性上班近的Andr​​oid版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆