使用SpannableString [/生成器]和富文本 [英] Using SpannableString[/Builder] and Rich-text

查看:201
本文介绍了使用SpannableString [/生成器]和富文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图构建富文本,其中第一部分说,寻找,并加粗,但其余不加粗。这是我的code:

I'm attempting to construct rich-text where the first part says "Looking for: " and is bolded, but the rest is NOT bolded. This is my code:

SpannableStringBuilder lookingForString = new SpannableStringBuilder("Looking for: ");
lookingForString.setSpan(new StyleSpan(Typeface.BOLD), 0, lookingForString.length(),
                                         0);
int start = (lookingForString.length() - 1);
for (int i = 0; i < looking_for_names.length(); ++i) {
// No comma before the first and after the last 'Looking for' value
    if (i > 0) {
        lookingForString.append(", ");
        lookingForString.setSpan(new StyleSpan(Typeface.NORMAL), start,
                                (lookingForString.length() - 1), 0);
        start += 2;
    }

    String lfItem = looking_for_names.optString(i);
    lookingForString.append(lfItem);
    lookingForString.setSpan(new StyleSpan(Typeface.NORMAL), start,
                            (lookingForString.length() - 1), 0);
    start += lfItem.length();
}
tvLookingFor.setText(lookingForString, BufferType.SPANNABLE);

但是,结果是,整个行被加粗。我试过许多变化,但我不能设法正确地控制字体...它会保留第一字体不管我怎么code吧。

However, the result is that the entire line is bolded. I've tried many variations, but I cannot manage to properly control the typefaces... it tends to retain the first typeface no matter how I code it.

我如何获得唯一的寻找:要大胆,但文字的其余部分是常规(非粗体)

How do I get only "Looking for: " to be bold, but the rest of the text to be regular (non-bolded)?

推荐答案

如果你只需要粗体字的一部分,那么只需使用html标签。

If you just need to Bold the part of text, then just use html tags.

String htmlString="<b>Looking for: Bold</b> and Normal";
tvLookingFor.setText(Html.fromHtml(htmlString), TextView.BufferType.SPANNABLE);

这篇关于使用SpannableString [/生成器]和富文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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