在安卓中创建自定义TextView以显示不同颜色的单词

15

是否可以使textview中的每个单词或每个字母都有不同的颜色?我尝试扩展textview并创建它,但我遇到的问题是如何同时绘制所有文本并使用不同的颜色。


是的,使用Html.fromHtml()。请参见https://dev59.com/V3I_5IYBdhLWcg3wFvDL。 - Xavi Gil
谢谢Xavi。在我的情况下,Html.fromHtml()似乎有效。我以前从未尝试过可扩展的spannable,所以我会先尝试一下html。 - wtsang02
3个回答

33
使用 android.text.Spannable
final SpannableStringBuilder str = new SpannableStringBuilder(text);
str.setSpan(
    new ForegroundColorSpan(Color.BLUE), 
    wordStart, 
    wordEnd, 
    SpannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE
);
myTextView.setText(str);

编辑:将所有的“Java”变为绿色

final Pattern p = Pattern.compile("Java");
final Matcher matcher = p.matcher(text);

final SpannableStringBuilder spannable = new SpannableStringBuilder(text);
final ForegroundColorSpan span = new ForegroundColorSpan(Color.GREEN);
while (matcher.find()) {
    spannable.setSpan(
        span, matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
    );
}
myTextView.setText(spannable);

如果我想让TextView中所有的“Java”都变成绿色,我该如何设置span? - wtsang02
要做到这一点,您必须使用正则表达式。请参见编辑后的答案。 - vasart
还有一个跟进的问题。我能把“spannable”放到另一个循环中吗?并将另一个字符串设置为不同的颜色? - wtsang02
1
SpannableStringBuilder是最好的选择。 - Code Droid
非常感谢。我已经解决了大部分问题。 - wtsang02

21
SpannableString类允许您通过应用CharacterStyle(例如ForegroundColorSpan)的扩展来使用setSpan方法轻松地格式化字符串的某些部分(跨度),使一个部分和另一个部分看起来不同。

您可以尝试这样做:

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    richTextView = (TextView)findViewById(R.id.rich_text);

    // this is the text we'll be operating on
    SpannableString text = new SpannableString("Lorem ipsum dolor sit amet");

    // make "Lorem" (characters 0 to 5) red
    text.setSpan(new ForegroundColorSpan(Color.RED), 0, 5, 0);

    // make "ipsum" (characters 6 to 11) one and a half time bigger than the textbox
    text.setSpan(new RelativeSizeSpan(1.5f), 6, 11, 0);

    // make "dolor" (characters 12 to 17) display a toast message when touched
    final Context context = this;
    ClickableSpan clickableSpan = new ClickableSpan() {
        @Override
        public void onClick(View view) {
            Toast.makeText(context, "dolor", Toast.LENGTH_LONG).show();
        }
    };
    text.setSpan(clickableSpan, 12, 17, 0);

    // make "sit" (characters 18 to 21) struck through
    text.setSpan(new StrikethroughSpan(), 18, 21, 0);

    // make "amet" (characters 22 to 26) twice as big, green and a link to this site.
    // it's important to set the color after the URLSpan or the standard
    // link color will override it.
    text.setSpan(new RelativeSizeSpan(2f), 22, 26, 0);
    text.setSpan(new URLSpan("http://www.chrisumbel.com"), 22, 26, 0);
    text.setSpan(new ForegroundColorSpan(Color.GREEN), 22, 26, 0);

    // make our ClickableSpans and URLSpans work
    richTextView.setMovementMethod(LinkMovementMethod.getInstance());

    // shove our styled text into the TextView        
    richTextView.setText(text, BufferType.SPANNABLE);
}
结果将会像这样:

enter image description here

更多细节请参见Chris Umbel的博客

4

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接