Android 在 <br> HTML.fromHTML 之后打印



我正在使用Wordpress API在我的应用程序中获取帖子。我正在尝试在写帖子时添加新行。但是,例如,当我添加这样的帖子时:

这是/n 一个帖子

在我的应用程序中,我可以在文本视图中看到/n,就像我上面所说的那样。当我尝试使用<br>标签时,也会发生同样的事情。

在文本视图中设置帖子:

String content = String.valueOf(HTML.fromHTML(p.excerpt));
vItem.shortContent.setText(content);

为什么?

编辑!!!

添加后它正在工作:

String content = p.excerpt.replace("\n","<br>);
vItem.short_content.setText(Html.fromHtml(content));

谢谢!

假设p.excerpt是一个HTMLString,使用

vItem.shortContent.setText(Html.fromHtml(p.excerpt));

相反。

您看到的是新行String.valueOf(Object)因为 param 对象调用toString()。由于 Html.fromHtml(( 返回一个Spannable,你的代码正在执行一个spannable.toString()所以它返回文字文本。

你也可以把HTML格式的文本放在字符串资源目录中,如下所示

<string name="helloworld">
<![CDATA[
This is now formatted:<br />
<strong>Hello world</strong> - this should work fine
]]>
</string>


然后,您可以在活动中调用该字符串,如下所示

txtView = findViewById(R.id.mytext);
String formatedstring = getString(R.string.helloworld);
Spanned txt = Html.fromHtml(formatedstring);
txtView.setText(txt);

最新更新