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 * 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?
在android中是否有设置字符串资源的颜色?我的意思是,我知道我可以使用一些html标签来改变字符串风格(或子字符串),但是我没有发现任何可以改变颜色的标签。我在*上见过其他解决方案,比如在设置文本之前将字符串传递给Html.fromHtml(string),但我想在字符串资源编辑器中进行。有可能吗?
4 个解决方案
#1
10
As far as I know it is not possible. I would use a SpannableString to change the color.
据我所知,这是不可能的。我将使用一个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 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.
Spannable真的很不错。你可以设置像fontcapture之类的东西把它附加到文本视图。优点是您可以在一个视图中使用不同的颜色。
Edit: Ok, if you only want to set the Color the solution mentioned above me is the way to go.
编辑:好的,如果你只想设置颜色,我上面提到的解决方案就是最好的办法。
#2
19
It looks like this method is working:
看起来这个方法是有效的:
<string name="some_text">this is <font fgcolor="#ffff0000">red</font></string>
#3
0
The strings themselves have no color, but you can change the color of the text in the textView they appear in. See the textview documentation, but there are 2 ways to do it.
字符串本身没有颜色,但是您可以在它们所显示的textView中更改文本的颜色。请参阅textview文档,但是有两种方法。
XML
XML
android:textColor
Code
代码
setTextColor(int)
#4
0
I have recently made a flexible solution for this problem. It enables me of easily add multiple styles to substrings by using method chaining. It makes use of the SpannableString. When you want to give a certain substring a color, you can use ForegroundColorSpan.
我最近针对这个问题提出了一个灵活的解决方案。通过使用方法链接,可以轻松地向子字符串添加多种样式。它利用了SpannableString。当你想要给某个子字符串一个颜色时,你可以使用前置色距。
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;
}
For full code see gist.
有关完整的代码,请参阅要点。
#1
10
As far as I know it is not possible. I would use a SpannableString to change the color.
据我所知,这是不可能的。我将使用一个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 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.
Spannable真的很不错。你可以设置像fontcapture之类的东西把它附加到文本视图。优点是您可以在一个视图中使用不同的颜色。
Edit: Ok, if you only want to set the Color the solution mentioned above me is the way to go.
编辑:好的,如果你只想设置颜色,我上面提到的解决方案就是最好的办法。
#2
19
It looks like this method is working:
看起来这个方法是有效的:
<string name="some_text">this is <font fgcolor="#ffff0000">red</font></string>
#3
0
The strings themselves have no color, but you can change the color of the text in the textView they appear in. See the textview documentation, but there are 2 ways to do it.
字符串本身没有颜色,但是您可以在它们所显示的textView中更改文本的颜色。请参阅textview文档,但是有两种方法。
XML
XML
android:textColor
Code
代码
setTextColor(int)
#4
0
I have recently made a flexible solution for this problem. It enables me of easily add multiple styles to substrings by using method chaining. It makes use of the SpannableString. When you want to give a certain substring a color, you can use ForegroundColorSpan.
我最近针对这个问题提出了一个灵活的解决方案。通过使用方法链接,可以轻松地向子字符串添加多种样式。它利用了SpannableString。当你想要给某个子字符串一个颜色时,你可以使用前置色距。
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;
}
For full code see gist.
有关完整的代码,请参阅要点。