如何在Android TextView中添加换行符?

问题描述 投票:137回答:22

我试图在TextView中添加换行符。

我试过建议\ n但是什么也没做。这是我设置文本的方式。

TextView txtSubTitle = (TextView)findViewById(r.id.txtSubTitle);
txtSubTitle.setText(Html.fromHtml(getResources().getString(R.string.sample_string)));

这是我的字符串:<string name="sample_string">some test line 1 \n some test line 2</string>

它应该如此显示:

some test line 1
some test line 2

但它显示如下:some test line 1 some test line 2

我错过了什么吗?

android textview line line-breaks
22个回答
200
投票

\n为我工作,像这样:

<TextView android:text="First line\nNext line"

9
投票

二手Android Studio 0.8.9。对我有用的唯一方法是使用\n。 CDATA,<br><br />都没有包装。


8
投票

我使用以下内容:

YOUR_TEXTVIEW.setText("Got some text \n another line");

8
投票

很简单:使用“\ n”

    String aString1 = "abcd";
    String aString2 = "1234";
    mSomeTextView.setText(aString1 + "\n" + aString2);

\ n对应于ASCII字符0xA,即“LF”或换行符

\ r \ n对应于ASCII字符0xD,即'CR'或回车符

这可以追溯到第一台打字机,你可以选择只换行(并只键入一行),或换行+回车(也移动到行的开头)

在Android / java上,\ n对应于回车符+换行符,否则只会“覆盖”同一行


6
投票

尝试仔细检查您的本地化。可能,你试图编辑一个文件(本地化),但实际上编程使用另一个,just like in my case。默认的系统语言是俄语,而我试图编辑英语本地化。

就我而言,工作解决方案是使用“\ n”作为行分隔符:

    <string name="string_one">line one.
    \nline two;
    \nline three.</string>

4
投票

您也可以添加"&lt;br&#47;&gt;"而不是\ n。

它是用于转义的HTML转义代码

然后您可以向TextView添加文本:

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

3
投票

您还可以使用Android Studio的String-Editor,它会自动生成线制动器和类似的东西......


2
投票

由于Html.fromHtml已弃用,我只是使用此代码在下一行中获取String2。

textView.setText(fromHtml("String1 <br/> String2"));

.

@SuppressWarnings("deprecation")
    public static Spanned fromHtml(String html){
        Spanned result;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            result = Html.fromHtml(html,Html.FROM_HTML_MODE_LEGACY);
        } else {
            result = Html.fromHtml(html);
        }
        return result;
    }

2
投票

最简单的方法是转到值/字符串(在资源文件夹中)

在那里声明一个字符串:

    <string name="example_string">Line 1\Line2\Line n</string>

在您的特定xml文件中,只需调用字符串即可

    <TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/example_string" />

1
投票

我发现了另一种方法:必须添加“android:maxWidth =”40dp“”属性。当然,它可能无法完美地工作,但它提供了一个换行符。


1
投票

\ n不适合我。我能够通过将xml更改为文本并构建textview文本属性来解决问题,如下所示。

android:text="Line 1
Line 2
Line 3

DoubleSpace"

希望这可以帮助那些说不适合他们的人。


83
投票

好吧想通了:

<string name="sample_string"><![CDATA[some test line 1 <br />some test line 2]]></string>

所以在CDATA中包装是必要的,并且在html标签内部添加了break


1
投票

我正在从文件中读取文本,所以我采用了稍微不同的方法,因为在文件中添加\ n会导致文本中出现\ n。

    final TextView textView = (TextView) findViewById(R.id.warm_up_view);
    StringBuilder sb = new StringBuilder();
    Scanner scanner = new Scanner(getResources().openRawResource(R.raw.warm_up_file));
    while (scanner.hasNextLine()) {
      sb.append(scanner.nextLine());
      sb.append("\n");
    }

    textView.setText(sb.toString());

0
投票

也许你能够将lf放入文本中,但它不显示?确保控制器有足够的高度。例如:

正确:

android:layout_height="wrap_content"

可能是错的:

android:layout_height="10dp"

0
投票

就我而言,我通过添加以下内容解决了这个问题:

android:inputType="textMultiLine"

66
投票

Android 1.6版无法识别\ r \ n。相反,使用:System.getProperty(“line.separator”)

String s = "Line 1"
           + System.getProperty ("line.separator")
           + "Line 2"
           + System.getProperty ("line.separator");

57
投票

仅当您将字符串资源值放在引号中时,换行符(\ n)才有效:

<string name="sample_string">"some test line 1 \n some test line 2"</string>

如果你没有这样的引号,它不会做换行:

<string name="sample_string">some test line 1 \n some test line 2</string>

是的,就这么简单。


29
投票

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

string = string.replace("\\\n", System.getProperty("line.separator"));
  1. 使用替换方法,您需要过滤转义的换行符(例如'\\ n')
  2. 只有这时换行'\ n'转义字符的每个实例都会被渲染到实际的换行符中

在本例中,我使用了带有JSON格式数据的Google Apps Scripting noSQL数据库(ScriptDb)。

干杯:D


16
投票

有两种方法可以解决这个问题。如果将字符串用作原始字符串,则需要使用换行符。如果您将其用作html,例如通过使用Html.fromString解析它,第二个变体更好。

1)换行符\n

<string name="sample> This\nis a sample</string>

2)Html换行标签<br><br />

<string name="sample> This<br>is a sample</string>

16
投票

这对我有用

android:text="First \n Second"

15
投票

这对我有用,也许有人会发现这有用:

TextView textField = (TextView) findViewById(R.id.textview1);
textField.setText("First line of text" + System.getProperty("line.separator") + "Linija 2");

9
投票

如果您使用XML来声明TextView使用android:singleLine = "false"或Java,请使用txtSubTitle.setSingleLine(false);

© www.soinside.com 2019 - 2024. All rights reserved.