【问题标题】:Custom TextView in android with different color wordsandroid中的自定义TextView具有不同的颜色词
【发布时间】:2012-07-13 20:09:24
【问题描述】:

是否可以让textview 为每个单词设置不同的颜色?甚至每一个字母?我尝试扩展 textview 并创建它,但我想到的问题是,我如何用不同的颜色同时绘制所有文本?

【问题讨论】:

  • 是的,Html.fromHtml()stackoverflow.com/q/1529068/871102
  • 谢谢哈维。 Html.fromHtml() 似乎适用于我的情况。从未尝试过 spannable,所以我在 html 之前先尝试一下。

标签: android textview


【解决方案1】:

使用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?
  • 为此,您必须使用正则表达式。请参阅编辑后的答案。
  • 还有一个后续问题。我可以将“spannable”放入另一个循环吗?并将另一个字符串设置为不同的颜色?
  • SpanableStringBuilder 是要走的路。
  • 非常感谢。我解决了大部分问题。
【解决方案2】:

SpannableString 类允许您通过 setSpan 方法应用 CharacterStyle 的扩展(即ForegroundColorSpan),轻松地以一种方式格式化字符串的某些部分(跨度)和另一种格式。

你可以试试这个:

@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);
}

结果将如下所示:

如需了解更多详情,请参阅 Chris Umbel blog

【讨论】:

    【解决方案3】:

    是的,您可以使用 SpannableSpannableStringBuilder 执行此操作。一个例子请参见Is there any example about Spanned and Spannable text

    有关格式化文本的各种方式(背景色、前景色、可点击等),请参阅CharacterStyle

    【讨论】:

      猜你喜欢
      • 2021-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-10
      • 2017-04-14
      • 1970-01-01
      • 2017-05-14
      相关资源
      最近更新 更多