使用此方法设置文本后,我似乎得到了一些额外的换行符TextView
message.setText(Html.fromHtml( message ));
我怎样才能删除这些?它们导致我的布局扭曲,因为它在输出中添加了两行额外的行。
该字符串通过以下方式保存到我的 sqlite 数据库中Html.toHtml( editText.getText() ).trim();
初始字符串输入:hello
消息变量的日志输出:<p dir="ltr">hello</p>
使用此方法设置文本后,我似乎得到了一些额外的换行符TextView
message.setText(Html.fromHtml( message ));
我怎样才能删除这些?它们导致我的布局扭曲,因为它在输出中添加了两行额外的行。
该字符串通过以下方式保存到我的 sqlite 数据库中Html.toHtml( editText.getText() ).trim();
初始字符串输入:hello
消息变量的日志输出:<p dir="ltr">hello</p>
你可以使用这条线......完全有效;)
我知道您的问题已解决,但也许有人觉得这很有用。
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();
}
看起来 toHtml 假设所有内容都应该在<p>
标签中。在写入数据库之前,我会去掉开头和结尾<p>
以及标签。</p>
对于 kotlin,您可以使用
html.trim('\n')
这在Kotlin
.
val myHtmlString = "<p>Test<\/p>"
HtmlCompat.fromHtml(myHtmlString.trim(), FROM_HTML_MODE_COMPACT).trim('\n')