如何让SpannableStringBuilder附加格式化字符串内的跨度? [英] How to have a SpannableStringBuilder append a span that's inside a formatted string?

查看:124
本文介绍了如何让SpannableStringBuilder附加格式化字符串内的跨度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我使用SpannableStringBuilder将多个内容附加到其中,其中之一是我从strings.xml文件格式化的字符串,该文件在内部具有跨度:

Suppose I use SpannableStringBuilder to append multiple stuff into it, and one of them is string that I format from the strings.xml file, which has a span inside:

SpannableStringBuilder stringBuilder = new SpannableStringBuilder ();
stringBuilder.append(...)...

final SpannableString span = new SpannableString(...);
span.setSpan(new BackgroundColorSpan(0xff990000), ...,...,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
stringBuilder.append(getString(R.string.string_to_format, span));

stringBuilder.append(...)...
textView.setText(stringBuilder);

问题

不幸的是,格式化这样的字符串会删除跨度本身,因此在我的情况下,不会有任何带有背景颜色的文本.

The problem

Sadly, formatting such a string removes the span itself, so in my case, there won't be any text with a background color.

这发生在"getString"行上.

This happens on the line of the "getString".

如果我只是单独附加跨度(不带"getString"),则可以正常工作.

If I just append the span alone (without "getString"), it works fine.

我还尝试研究Html.fromHtml,但无论如何它似乎都不支持文本的背景色.

I also tried to investigate Html.fromHtml, but it doesn't seem to support a background color for text anyway.

是否可以格式化具有跨度但仍在跨度内的字符串?

Is it possible to format a string that has a span, yet still have the span within?

更具体地说,输入是来自strings.xml文件的字符串A,该字符串仅具有一个占位符(没有特殊的HTML标记),另一个是应该在运行时替换占位符的字符串B.字符串B的一部分文本应突出显示.

More specifically, the input is a string A from the strings.xml file, which only has a placeholder (no special HTML tags), and another string B that is supposed to replace the placeholder at runtime. The string B should have a highlight for a partial text of itself.

就我而言,突出显示的文本是要在字符串B中搜索的内容.

In my case, the highlighted text is a something to search for within string B.

推荐答案

好的,我已经找到了解决我特殊情况的答案,但是我仍然想知道是否有更好的方法.

OK, I've found an answer to my special end case, but I'd still like to know if there are better ways.

这就是我所做的:

String stringToSearchAt=...
String query=...
int queryIdx = stringToSearchAt.toLowerCase().indexOf(query);
stringToSearchAt= stringToSearchAt.substring(0,  queryIdx + query.length()) + "<bc/>" + stringToSearchAt.substring(queryIdx + query.length());
final String formattedStr=getString(..., stringToSearchAt);
stringBuilder.append(Html.fromHtml(formattedStr, null, new TagHandler() {
                                    int start;

                                    @Override
                                    public void handleTag(final boolean opening, final String tag, Editable output, final XMLReader xmlReader) {
                                        switch (tag) {
                                            case "bc":
                                                if (!opening)
                                                    start = output.length() - query.length();
                                                break;
                                            case "html":
                                                if (!opening)
                                                    output.setSpan(new BackgroundColorSpan(0xff00bbaa), start, start + query.length(), 0);
                                        }
                                    }
                                }));

这仅对我的情况有利,但是对于常规格式而言,这是不够的.

This is only good for my case, but in the case of general formatting, this won't suffice.

这篇关于如何让SpannableStringBuilder附加格式化字符串内的跨度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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