9

我正在设置Span部分文本。跨度本身运作良好。然而,文本是由String.formatfrom创建的Resources,我不知道start并且end我将设置 Span 到的文本的一部分。

我尝试在 中使用自定义 HTML 标记strings.xml,但要么删除它们,getText要么getString删除它们。getString(R.string.text, "<nb>" + arg + "</nb>")那么我可以使用这样的东西Html.fromHtml(),因为这arg正是我想要设置跨度的地方。

我看到了这种使用文本格式的方法"normal text ##span here## normal text"。它解析字符串删除标签并设置跨度。

我的问题是,有没有更好的方法来完成将 Span 设置为格式化字符串,"something %s something"或者我应该使用上述方法之一?

4

4 回答 4

7

getText()将返回SpannedString包含在 strings.xml 中定义的格式的对象。我创建了一个自定义版本String.format,它将保留格式字符串中的任何跨度,即使它们包含格式说明符(SpannedString参数中的跨度也被保留)。像这样使用它:

Spanned toDisplay = SpanFormatter.format(getText(R.string.foo), bar, baz, quux);
于 2014-11-30T07:23:33.547 回答
2

我通过引入TaggedArg类解决了这个问题,这个类的实例扩展为<tag>value</tag>. 然后我创建了负责读取包含标签的文本并用跨度替换这些标签的对象。在 map tag->factory 中注册了不同的 span。

有一点小惊喜。如果您有类似的文字"<xx>something</xx> something",请Html.fromHtml将此文字读作"<xx>something something</xx>"。我不得不<html>在整个文本周围添加标签以防止这种情况。

于 2011-09-05T07:17:14.063 回答
1

我决定写一个由 George 提供的 Kotlin 版本以防链接有一天消失:

/*
* Copyright © 2014 George T. Steel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//https://github.com/george-steel/android-utils/blob/master/src/org/oshkimaadziig/george/androidutils/SpanFormatter.java
/**
 * Provides [String.format] style functions that work with [Spanned] strings and preserve formatting.
 *
 * @author George T. Steel
 */
object SpanFormatter {
    private val FORMAT_SEQUENCE: Pattern = Pattern.compile("%([0-9]+\\$|<?)([^a-zA-z%]*)([[a-zA-Z%]&&[^tT]]|[tT][a-zA-Z])")

    /**
     * Version of [String.format] that works on [Spanned] strings to preserve rich text formatting.
     * Both the `format` as well as any `%s args` can be Spanned and will have their formatting preserved.
     * Due to the way [android.text.Spannable]s work, any argument's spans will can only be included **once** in the result.
     * Any duplicates will appear as text only.
     *
     * @param format the format string (see [java.util.Formatter.format])
     * @param args
     * the list of arguments passed to the formatter. If there are
     * more arguments than required by `format`,
     * additional arguments are ignored.
     * @return the formatted string (with spans).
     */
    fun format(format: CharSequence?, vararg args: Any?): SpannedString {
        return format(java.util.Locale.getDefault(), format, *args)
    }

    /**
     * Version of [String.format] that works on [Spanned] strings to preserve rich text formatting.
     * Both the `format` as well as any `%s args` can be Spanned and will have their formatting preserved.
     * Due to the way [android.text.Spannable]s work, any argument's spans will can only be included **once** in the result.
     * Any duplicates will appear as text only.
     *
     * @param locale
     * the locale to apply; `null` value means no localization.
     * @param format the format string (see [java.util.Formatter.format])
     * @param args
     * the list of arguments passed to the formatter.
     * @return the formatted string (with spans).
     * @see String.format
     */
    fun format(locale: java.util.Locale, format: CharSequence?, vararg args: Any?): SpannedString {
        val out = SpannableStringBuilder(format)
        var i = 0
        var argAt: Int = -1
        while (i < out.length) {
            val m: java.util.regex.Matcher = FORMAT_SEQUENCE.matcher(out)
            if (!m.find(i))
                break
            i = m.start()
            val exprEnd: Int = m.end()
            val argTerm: String? = m.group(1)
            val modTerm: String? = m.group(2)
            val typeTerm: String? = m.group(3)
            var cookedArg: CharSequence
            when (typeTerm) {
                "%" -> cookedArg = "%"
                "n" -> cookedArg = "\n"
                else -> {
                    val argIdx: Int = when (argTerm) {
                        "" -> ++argAt
                        "<" -> argAt
                        else -> argTerm!!.substring(0, argTerm.length - 1).toInt() - 1
                    }
                    val argItem: Any? = args[argIdx]
                    cookedArg = if ((typeTerm == "s") && argItem is Spanned) {
                        argItem
                    } else {
                        String.format(locale, "%$modTerm$typeTerm", argItem)
                    }
                }
            }
            out.replace(i, exprEnd, cookedArg)
            i += cookedArg.length
        }
        return SpannedString(out)
    }
}
于 2020-11-05T00:12:28.230 回答
0

getText()乔治设想的方式很有趣。但是没有必要编写额外的类。getText()返回一个CharSequence。所以用它SpannableStringBuilder来设置一个TextView:

textViewCell.setText(new SpannableStringBuilder(getText(R.string.foo));

在您的 strings.xml 中,您可以使用 html-tags 编写它(不要忘记文本前后的引号):

<string name="foo">"CO<sub><small>2</small></sub>"</string>

这种标记对我有用,要么手动将其设置为 textView(如上),要么在布局中分配它。

于 2016-04-13T09:19:35.300 回答