How do I add a newline to a TextView in Android?

2019-01-04 09:15发布

When I define a TextView in xml, how do I add a new line to it? \n seems not to work.

<TextView
   android:id="@+id/txtTitlevalue"
   android:text="Line1: \n-Line2\n-Line3"
   android:layout_width="54dip"
   android:layout_height="fill_parent"
   android:textSize="11px" />

26条回答
时光不老,我们不散
2楼-- · 2019-01-04 09:57

Make sure your \n is in "\n" for it to work.

查看更多
Emotional °昔
3楼-- · 2019-01-04 09:59

For me the solution was to add the following line to the layout:

<LinearLayout
    xmlns:tools="http://schemas.android.com/tools"
    ...
    >

And \n shows up as a new line in the visual editor. Hope it helps!

查看更多
登录 后发表回答