在资源字符串修改字的颜色在资源字符串修改字的颜色(Change word color in reso

2019-05-12 03:20发布

反正是有设定在Android的一个字符串资源的颜色? 我的意思是,我知道我可以使用某些HTML标记来改变字符串装饰(或子),但没有发现任何变色。 我在这里看到其他的解决方案,在如设置文本之前的字符串传递给Html.fromHtml(串),计算器,但我想这样做的字符串资源编辑器。 任何可能性?

Answer 1:

据我知道这是不可能的。 我会用一个SpannableString改变颜色。

    int colorBlue = getResources().getColor(R.color.blue);
    String text = getString(R.string.text);
    SpannableString spannable = new SpannableString(text);
    // here we set the color
    spannable.setSpan(new ForegroundColorSpan(colorBlue), 0, text.length(), 0);

Spannable是非常好的。 您可以设置这样想fontseize和东西存在,只是它附加到一个文本视图。 其优点是,你可以在一个视图中有不同的颜色。

编辑:好吧,如果你只是想设置的颜色我上面提到的解决方案是要走的路。



Answer 2:

它看起来像这种方法工作:

    <string name="some_text">this is <font fgcolor="#ffff0000">red</font></string>


Answer 3:

字符串本身没有颜色,但你可以改变它们出现在TextView的文本的颜色。参见TextView的文档 ,但也有2种方法来做到这一点。

XML

android:textColor

setTextColor(int)


Answer 4:

我最近提出了这个问题的灵活的解决方案。 它使我很容易地通过使用方法链接添加多个样式子。 它利用SpannableString的。 当你想给某个子颜色,你可以使用ForegroundColorSpan。

public StyledString putColor(String subString, @ColorRes int colorRes){
    if(getStartEnd(subString)){
        int color = ColorUtil.getColor(context, colorRes);
        ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(color);
        fullStringBuilder.setSpan(foregroundColorSpan, startingIndex, endingIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    return this;
}

对于完整的代码见要点 。



文章来源: Change word color in resource string