【问题标题】:How can I write text with subscripts and superscripts and special characters?如何编写带有下标和上标以及特殊字符的文本?
【发布时间】:2017-09-12 07:25:25
【问题描述】:

我曾尝试使用Html.fromHtml(String text),但似乎Html.fromHtml() 已被弃用且不起作用。我该如何解决这个问题?

这里你可以看到一个例子:

对不起,原来是这个意思:

TextView tv;

private void function(String data){

    if (data.equals("a")) {
        newString+="a";

    } else if (data.equals("x^2")) {
        newString += Html.fromHtml("x<sup><small>2</small></sup>")
    }
}
tv.setText(newString);

结果不是x2,而是x2。

【问题讨论】:

  • 请提供minimal reproducible example 证明您正在尝试fromHtml()详细说明“odes not work”是什么意思。
  • @CommonsWare 对不起,我已经编辑了这个问题,使它更容易理解。谢谢。

标签: android html unicode encoding


【解决方案1】:

摆脱newString。或者,将其更改为CharSequence,并使用TextUtils.concat()Html.fromHtml() 输出附加到它。就目前而言,您正在格式化文本......然后通过将其转换为 String 来删除格式。

【讨论】:

    【解决方案2】:

    Html.fromHtml()替换为以下代码:

    @SuppressWarnings("deprecation")
    public static Spanned fromHtml(String html){
        Spanned result;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
           result = Html.fromHtml(html,Html.FROM_HTML_MODE_LEGACY);
        } else {
           result = Html.fromHtml(html);
        }
        return result;
    }
    

    【讨论】:

      猜你喜欢
      • 2016-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-11
      相关资源
      最近更新 更多