【问题标题】:Html tag is not supporting "\n"(new line) in androidhtml标签不支持android中的“\n”(新行)
【发布时间】:2015-08-21 09:42:39
【问题描述】:
static String test2 = " <h1> THis is the \n test </h1>";
TextView tst;
tst.setText(Html.fromHtml(test2));
输出:
这是测试
所需输出:
这是
测试
不将“\n”替换为“
”有可能吗??
【问题讨论】:
标签:
android
html
newline
line-breaks
【解决方案1】:
\n 和 <br> 之间存在差异。
"<br>" 是换行符的 HTML 标签,而\n 是 Unix 标签,它将在渲染的源代码中显示换行符,但在大多数情况下不会在渲染的视图中显示。
【解决方案2】:
\n 在 HMTL 中不起作用,您应该在 html 中使用<br> 标记换行
static String test2 = " <h1> THis is the <br> test </h1>";
【解决方案3】:
您也可以通过将 \n 放在双引号中来在 XML 中执行此操作
制作字符串资源
<string name="htmltext"><u><i>Project Title</i></u><u>Smart City Apps</u>+"\n"+Developed by Stack.</string>
android:text="@string/htmltext"/>