3

使用此方法设置文本后,我似乎得到了一些额外的换行符TextView

message.setText(Html.fromHtml( message ));

我怎样才能删除这些?它们导致我的布局扭曲,因为它在输出中添加了两行额外的行。

该字符串通过以下方式保存到我的 sqlite 数据库中Html.toHtml( editText.getText() ).trim();

初始字符串输入:hello

消息变量的日志输出:<p dir="ltr">hello</p>

4

4 回答 4

3

你可以使用这条线......完全有效;)

我知道您的问题已解决,但也许有人觉得这很有用。

 try{
        string= replceLast(string,"<p dir=\"ltr\">", "");
        string=replceLast(string,"</p>", "");
}catch (Exception e) {}

这里是 replaceLast ...

public String replceLast(String yourString, String frist,String second)
{
    StringBuilder b = new StringBuilder(yourString);
    b.replace(yourString.lastIndexOf(frist), yourString.lastIndexOf(frist)+frist.length(),second );
    return b.toString();
}
于 2014-07-03T11:56:15.587 回答
1

看起来 toHtml 假设所有内容都应该在<p>标签中。在写入数据库之前,我会去掉开头和结尾<p>以及标签。</p>

于 2014-03-15T03:36:45.487 回答
1

对于 kotlin,您可以使用

html.trim('\n')
于 2019-03-10T11:19:12.520 回答
0

这在Kotlin.

val myHtmlString = "<p>Test<\/p>"

HtmlCompat.fromHtml(myHtmlString.trim(), FROM_HTML_MODE_COMPACT).trim('\n')
于 2021-10-14T05:30:18.457 回答