Pen*_*m10 241 android newline textview
当我定义一个TextViewin时xml,如何添加一个新行?\n好像不行.
<TextView
android:id="@+id/txtTitlevalue"
android:text="Line1: \n-Line2\n-Line3"
android:layout_width="54dip"
android:layout_height="fill_parent"
android:textSize="11px" />
Run Code Online (Sandbox Code Playgroud)
小智 36
我认为这与您的HTM.fromHtml(subTitle)调用有关:"\n"并不意味着对HTML进行bupkis.尝试<br/>而不是"\n".
aF.*_*aF. 19
首先,将它放在textview中:
android:maxLines="10"
Run Code Online (Sandbox Code Playgroud)
然后\n在textview的文本中使用.
maxLines使TextView最多只有很多行.你可以选择另一个号码:)
小智 16
尝试了以上所有,做了一些我自己的研究,得到了以下解决方案,用于渲染换行符转义:
string = string.replace("\\\n", System.getProperty("line.separator"));
Run Code Online (Sandbox Code Playgroud)
使用replace方法,您需要过滤转义的换行符(例如'\\\n')
只有这时换行符'\n'转义字符的每个实例才会呈现到实际的换行符中
在本例中,我使用了带有JSON格式化数据的Google Apps Scripting noSQL数据库(ScriptDb).
干杯:D
小智 10
<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" />
Run Code Online (Sandbox Code Playgroud)
我认为这会奏效.
小智 8
我只是解决了同样的问题,将下面的属性放在xml中
android:lines="2" android:maxLines="4" android:singleLine="false"
Run Code Online (Sandbox Code Playgroud)
工作.Html.fromHtml("text1 <br> text2").toString()也工作.
我的2美分,使用Android TV.
\n在阅读时添加XML字符串:
public void setSubtitle(String subtitle) {
this.subtitle = subtitle.replace("\\n", System.getProperty("line.separator"));
}
Run Code Online (Sandbox Code Playgroud)
你也可以添加 <br>而不是\n.
然后你可以向 TexView 添加文本:
articleTextView.setText(Html.fromHtml(textForTextView));
Run Code Online (Sandbox Code Playgroud)
如果你调试,你会看到字符串实际上是"\ \r\ \n"or "\ \n",即它被转义了。因此,如果您按摩那根绳子以去除多余的\,您将获得解决方案。这是真的,尤其是当您从数据库中读取数据时。
小智 5
一种方法是使用Html标签::
txtTitlevalue.setText(Html.fromHtml("Line1"+"<br>"+"Line2" + " <br>"+"Line3"));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
330004 次 |
| 最近记录: |