为什么我的文字显示风格使用SpannableStringBuilder什么时候? [英] Why doesn't my text show up with style when using SpannableStringBuilder?

查看:154
本文介绍了为什么我的文字显示风格使用SpannableStringBuilder什么时候?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 SpannableString 对象的一个​​问题。

I have a problem with a SpannableString object.

下面是一个简单的例子:

Below's a short example:

SpannableString spanstr = new SpannableString("Bold please!");
spanstr.setSpan(new StyleSpan(Typeface.BOLD), 0, spanstr.length(), 0);

SpannableStringBuilder sb = new SpannableStringBuilder();
sb.append(spanstr);
sb.append("\n"); // A newline
sb.append("The first line is bold. This one isn't.");

CharSequence cq = sb.subSequence(0, sb.length());
// ^There's no such method to return a SpannableString,
// so I try to return a CharSequence instead.

// And then, at last:
TextView contentView = (TextView) findViewById(R.id.some_id);
contentView.setText(cq);

什么例子正试图做的是显示出这一点:

What the example's trying to do is showing this:

粗体请!
  第一行是大胆的。这一个不是。

Bold please!
The first line is bold. This one isn't.

但问题是:以粗体显示的文本的第一行不会出现

But the problem is: the first line of the text won't show up in bold.

为什么它不这么做预期?

Why doesn't it do it expected?

推荐答案

使用的spannable字符串生成器在TextView中设置为文本:

Use the spannable string builder for setting as text in textview :

contentView.setText(sb);

否则,你可以这样做:

or else you can do like this :

SpannableStringBuilder spanstr = new SpannableStringBuilder("Bold please!");
spanstr.setSpan(new StyleSpan(Typeface.BOLD), 0, spanstr.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spanstr.append("\n");
spanstr.append("The first line is bold. This one isn't.");
contentView.setText(spanstr);

这篇关于为什么我的文字显示风格使用SpannableStringBuilder什么时候?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆