【发布时间】:2013-12-08 11:23:02
【问题描述】:
我有一个ArrayList<String>,里面有很多词。我想在里面给特定的词上色,这可能吗?我怎样才能做到这一点?
ex. 我的ArrayList 中有这个:
我喜欢法拉利汽车,因为它们#Awesome!
我怎样才能让 Awesome 变成不同的颜色?以及如何为标点符号(和!)着色?
【问题讨论】:
我有一个ArrayList<String>,里面有很多词。我想在里面给特定的词上色,这可能吗?我怎样才能做到这一点?
ex. 我的ArrayList 中有这个:
我喜欢法拉利汽车,因为它们#Awesome!
我怎样才能让 Awesome 变成不同的颜色?以及如何为标点符号(和!)着色?
【问题讨论】:
试试这个..
第一个是
SpannableString WordtoSpan = new SpannableString("This is simple");
WordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//0 is starting character and 7 is ending character you need to specify it
textView.setText(WordtoSpan);
第二个是
String styledText = "This is <font color='red'>simple</font>.";
textView.setText(Html.fromHtml(styledText), TextView.BufferType.SPANNABLE);
编辑:
String txt = "partial colored #text";
int to_len = txt.length();
int sim_pos = txt.indexOf("#");
SpannableString WordtoSpan = new SpannableString(txt);
WordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), sim_pos, to_len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(WordtoSpan);
【讨论】:
您可以使用可扩展字符串,或者如果您使用 HTML,您可以从 HTML 文本加载文本视图。您可以在here 上查看示例
【讨论】: