当我定义一个TextView在xml,我如何添加一个新的行?\n似乎不起作用。
<TextView
android:id="@+id/txtTitlevalue"
android:text="Line1 -\nLine2"
android:layout_width="54dip"
android:layout_height="fill_parent"
android:textSize="11px" />
当我定义一个TextView在xml,我如何添加一个新的行?\n似乎不起作用。
<TextView
android:id="@+id/txtTitlevalue"
android:text="Line1 -\nLine2"
android:layout_width="54dip"
android:layout_height="fill_parent"
android:textSize="11px" />
当前回答
您需要将“\n”放在strings.xml文件中,而不是在页面布局中。
其他回答
在sting .xml中创建一个字符串
<resources>
...
<string name="new_line">\u000A</string>
</resources>
然后在代码中引用字符串
val linkString = "This is in the first line.${getString(R.string.new_line)}This is on the second line."
RuDrA05的答案很好,当我在eclipse上编辑XML时,它不起作用,但当我用notepad++编辑XML时,它确实起作用。
如果我读取一个用eclipse或notepad++保存的txt文件,也会发生同样的事情
可能和编码有关。
如果你调试,你会看到字符串实际上是“\ \r\ \n”或“\ \n”,也就是说,它被转义了。所以如果你按摩那条线,去掉多余的\,你就得到了解决方案。如果从数据库中读取数据,情况尤其如此。
试试这个:
android:text="Lorem Ipsum \nDolor Ait Amet \nLorem Ipsum"
也可以加上<br>而不是\n。
然后你可以添加文本到TexView:
articleTextView.setText(Html.fromHtml(textForTextView));