我尝试使用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);
结果不是x 2 ,而是x2。
答案 0 :(得分:3)
将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;
}
答案 1 :(得分:0)
摆脱newString
。或者,将其更改为CharSequence
,并使用TextUtils.concat()
将Html.fromHtml()
输出附加到其中。就目前而言,您正在格式化文本...然后通过将格式转换为String
来删除格式。