在资源字符串更改文字颜色字符串、颜色、文字、资源

2023-09-12 06:34:37 作者:神啊赐个妞吧

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

Is there anyway to set the color of a string resource in android? I mean, I know I can use some html tags to change string style (or substrings) but have not found any to change color. I have seen other solutions here at stackoverflow like passing the string to Html.fromHtml(string) before setting the text but I want to do it in the string resource editor. Any possibility?

推荐答案

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

As far as I know it is not possible. I would use a SpannableString to change the color.

    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和东西存在,只是其附加到一个文本视图。它的优点是,你可以有不同的颜色在一个视图中。

Spannable is really nice. You can set thinks like fontseize and stuff there and just attach it to a text view. The advantage is that you can have different colors in one view.

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

Ok, if you only want to set the Color the solution mentioned above me is the way to go.