【问题标题】:How do I add a newline to a TextView in Android?如何在 Android 中向 TextView 添加换行符?
【发布时间】:2010-05-15 15:17:44
【问题描述】:

当我在xml 中定义TextView 时,如何向其中添加新行? \n 好像不行。

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

【问题讨论】:

  • 你的代码对我有用。你得到什么而不是多行文本?
  • 在可视化编辑器中显示为文本\n

标签: android newline textview


【解决方案1】:

不要相信可视化编辑器。 您的代码确实可以在 emu 中运行。

【讨论】:

  • 好吧,就我而言,可视化编辑器说的是实话。它正在添加另一个反斜杠来显示反斜杠。正确的方法是将文本放入 xml 文件中。
  • 视觉编辑说:“你为什么不信任我?”哈哈
【解决方案2】:

试试:

android:lines="2"

\n 应该可以工作。

【讨论】:

  • 我的文本视图可能超过 2 行。它是一个控件。它可以是任何东西。
【解决方案3】:

试试System.getProperty("line.separator");

【讨论】:

    【解决方案4】:

    我认为这与您的HTM.fromHtml(subTitle) 通话有关:“\n”并不意味着对 HTML 的 bupkis。试试&lt;br/&gt; 而不是“\n”。

    【讨论】:

    • @androidevAlex 那是因为它是
      ,而不是
    【解决方案5】:

    首先,把它放在你的文本视图中:

    android:maxLines="10"
    

    然后在你的文本视图的文本中使用\n

    maxLines 使 TextView 最多有这么多行高。你可以选择另一个号码:)

    【讨论】:

      【解决方案6】:

      尝试了以上所有方法,我自己做了一些研究,得出了以下渲染换行符转义字符的解决方案:

      string = string.replace("\\\n", System.getProperty("line.separator"));
      
      1. 使用替换方法需要过滤转义换行符(例如'\\\n'

      2. 只有这样每个换行符 '\n' 转义字符的实例才会被渲染到实际的换行符中

      对于这个示例,我使用了带有 JSON 格式数据的 Google Apps Scripting noSQL 数据库 (ScriptDb)。

      干杯:D

      【讨论】:

      • 这对我有用:replace("\\n", System.getProperty("line.separator"));
      • 相反,string = string.replaceAll("\\\n", System.getProperty("line.separator"));
      【解决方案7】:

      确保您的 \n 位于 "\n" 中以使其正常工作。

      【讨论】:

        【解决方案8】:
        <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" />
        

        我认为这会奏效。

        【讨论】:

          【解决方案9】:

          我只是解决了同样的问题,把下面的属性放在xml中

          android:lines="2" android:maxLines="4" android:singleLine="false"
          

          工作。Html.fromHtml("text1 &lt;br&gt; text2").toString() 也工作。

          【讨论】:

          • 这仍然是相关的,谢谢!我有 maxLines,但我没有 singleLine=false。
          • 这是唯一对我有用的解决方案。谢谢!
          【解决方案10】:

          我的 2 美分,使用 Android TV。

          在 XML 字符串中添加\n,同时读取:

          public void setSubtitle(String subtitle) {
              this.subtitle = subtitle.replace("\\n", System.getProperty("line.separator"));
          }
          

          【讨论】:

            【解决方案11】:

            一种方法是使用Html标签::

            txtTitlevalue.setText(Html.fromHtml("Line1"+"<br>"+"Line2" + " <br>"+"Line3"));
            

            【讨论】:

              【解决方案12】:

              这解决了我的问题。

              stringVar.replaceAll("\\\\n", "\\\n");
              

              【讨论】:

                【解决方案13】:

                System.getProperty("line.separator"); 为我工作。

                【讨论】:

                  【解决方案14】:

                  如果你调试,你会看到字符串实际上是"\ \r\ \n""\ \n",即它被转义了。因此,如果您按摩该字符串,以摆脱额外的\,您将获得您的解决方案。尤其是当您从数据库中读取数据时尤其如此。

                  【讨论】:

                    【解决方案15】:

                    您也可以添加&amp;lt;br&amp;gt; 而不是\n.

                    然后你可以在 TexView 中添加文本:

                    articleTextView.setText(Html.fromHtml(textForTextView));
                    

                    【讨论】:

                    • 不能在 XML 中。它给出了编译错误 - android:text 不能包含 字符。
                    【解决方案16】:

                    您需要将"\n" 放在strings.xml 文件中,而不是在页面布局中。

                    【讨论】:

                    • 这对我有用,可视化编辑器正在添加额外的反斜杠,试图在 \n 中显示反斜杠
                    【解决方案17】:

                    需要保留

                    1.android:maxLines="no of lines"

                    2.并使用\n 获取下一行

                    【讨论】:

                      【解决方案18】:

                      对我来说,解决方案是在布局中添加以下行:

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

                      并且 \n 在可视化编辑器中显示为新行。希望对您有所帮助!

                      【讨论】:

                      • 我不确定它是否与问题有关:)
                      • 我遇到了同样的问题,字符串“\n”不是换行而是自己出现。当我在上面添加行时,情况不再如此,新行出现了
                      • 可能是因为您使用的是tools:text 而不是像OP 那样的android:text,但无论如何抱歉打扰了:)
                      • 我认为我应该更好地澄清它,感谢您的评论,我现在做了:) 我使用android:text 我认为这只是可视化编辑器的一个错误,就像接受答案所暗示的那样,但它是能够看到它更舒服:)
                      • 是的,我现在明白了,你说得对,它更舒服:) 谢谢
                      【解决方案19】:
                       android:text="Previous Line &#10; Next Line" 
                      

                      这会起作用。

                      【讨论】:

                      • 不,它没有。
                      • 你需要在模拟器中运行以查看它的工作。谢谢
                      【解决方案20】:

                      你需要把\n放到文件string.xml

                      <string name="strtextparkcar">press Park my Car to store location \n</string>
                      

                      【讨论】:

                        【解决方案21】:

                        试试这个:

                        android:text="Lorem Ipsum \nDolor Ait Amet \nLorem Ipsum"
                        

                        【讨论】:

                          【解决方案22】:

                          这很好用。检查您的应用程序。

                          <TextView
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:text="Text 1\nText 2\nText 3"/>
                          

                          【讨论】:

                            【解决方案23】:

                            试试吧:

                            Textview_name.settext("something \n  new line "):
                            

                            在 Java 文件中。

                            【讨论】:

                            • 请正确回答问题,否则使用评论
                            【解决方案24】:

                            RuDrA05 的回答很好,当我在 eclipse 上编辑 XML 时它不起作用,但是当我用 notepad++ 编辑 XML 时它确实起作用。

                            如果我读取用 eclipse 或 notepad++ 保存的 txt 文件,也会发生同样的事情

                            可能和编码有关。

                            【讨论】:

                              【解决方案25】:

                              旁注:使用大写文本

                              android:inputType="textCapCharacters"

                              或类似的东西似乎阻止了\n 的工作。

                              【讨论】:

                                【解决方案26】:

                                确保您使用的是your_package.R 而不是android.R

                                【讨论】:

                                  【解决方案27】:

                                  对于 TextView 中的新行,只需在文本中间添加 \n 它有效..

                                  【讨论】:

                                  • 但答案与问题link 不是“你的”问题有关...
                                  • 嘿,首先编辑您的答案。否则我不能改变我的投票。
                                  【解决方案28】:

                                  如果 TextView 在任何布局中(例如 LinearLayout),请尝试将方向属性更改为垂直为 android:orientation="vertical" 用于布局或更改 TextView 属性 android:singleLine="true" 以在其后创建新行。

                                  【讨论】:

                                    【解决方案29】:

                                    以编程方式:myTV.setText("My Text" + "\n" + myString);

                                    【讨论】:

                                      【解决方案30】:

                                      在 stings.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."
                                      

                                      【讨论】:

                                        猜你喜欢
                                        • 1970-01-01
                                        • 2011-02-19
                                        • 2011-05-11
                                        • 1970-01-01
                                        • 1970-01-01
                                        • 2011-09-03
                                        • 2014-06-01
                                        • 1970-01-01
                                        相关资源
                                        最近更新 更多