在XML中定义TextView
时,如何为其添加新行?\n
似乎不起作用。
<TextView
android:id="@+id/txtTitlevalue"
android:text="Line1 -\nLine2"
android:layout_width="54dip"
android:layout_height="fill_parent"
android:textSize="11px" />
在XML中定义TextView
时,如何为其添加新行?\n
似乎不起作用。
<TextView
android:id="@+id/txtTitlevalue"
android:text="Line1 -\nLine2"
android:layout_width="54dip"
android:layout_height="fill_parent"
android:textSize="11px" />
不要相信可视化编辑器。 你的代码在模拟器中有效。
尝试:
android:lines="2"
\n
应该有效。
尝试使用 System.getProperty("line.separator");
我认为这与你的 HTM.fromHtml(subTitle)
的调用有关:"\n" 对 HTML 来说没有任何意义。尝试使用 <br/>
替换 "\n"。
首先,将这个放在你的TextView里:
android:maxLines="10"
然后在TextView的文本中使用\n
。
maxLines属性使TextView最多只能有这么多行高。你可以选择其他数字 :)
尝试了上述所有方法,我自己进行了一些研究,得出了以下解决方案,以渲染换行符转义字符:
string = string.replace("\\\n", System.getProperty("line.separator"));
使用replace方法需要过滤转义的换行符(例如'\\\n'
)。
只有这样,每个换行符'\n'
转义字符的实例才能呈现为实际的换行符。
对于这个示例,我使用了一个Google Apps脚本的noSQL数据库(ScriptDb),其中数据格式为JSON。
干杯 :D
确保你的 \n
在 "\n"
中才能正常工作。
<TextView
android:id="@+id/txtTitlevalue"
android:text="Line1: \r\n-Line2\r\n-Line3"
android:layout_width="54dip"
android:layout_height="fill_parent"
android:textSize="11px" />
我认为这会起作用。
我个人建议,使用Android TV。
在读取XML字符串时,请添加\n
:
public void setSubtitle(String subtitle) {
this.subtitle = subtitle.replace("\\n", System.getProperty("line.separator"));
}
android:lines="2" android:maxLines="4" android:singleLine="false"
工作。Html.fromHtml("text1 <br> text2").toString()
也可以工作。