【问题标题】:Android TextView new line isn't workingAndroid TextView 新行不起作用
【发布时间】:2017-06-15 04:29:01
【问题描述】:

我找到了答案,我会在 2 天内接受它

我一直在尝试创建一个可以为用户显示多条消息的活动,为了使文本视图中的所有内容都适合我需要创建三行。我在网上搜索没有给我任何解决方案,这是我尝试过的

Java

"\n"
"\r\n"
newLineChar = System.getProperty("line.separator")
messageTextView.setText(Html.fromHtml("This\n Is\n A Test"));
messageTextView.setText(Html.fromHtml("This<br> Is<br> A Test"));

xml

android:lines="3"
android:maxLines="3"

杂项

  • 将字符串值直接硬编码到 setText() 中
  • 以上所有内容的各种组合
  • 移除 android:clickable="false"
  • 移除 android:cursorVisible="false"
  • 移除 android:focusable="false"
  • 移除 android:focusableInTouchMode="false"

代码sn-p:

// Message passed to next activity via putExtra()
message = "This\n Is\n A Test";
// Next Activity
TextView messageTextView = (TextView) findViewById(R.id.messageToUser);
String message = getIntent().getStringExtra("message");
messageTextView.setText(message);

TextView 的当前和更新的 XML:

<TextView
    android:id="@+id/messageToUser"
    android:layout_width="0dp"
    android:layout_height="200dp"
    android:background="#90FFFFFF"
    android:ems="10"
    android:fontFamily="serif"
    android:text=""
    android:textAlignment="center"
    android:textColor="@android:color/black"
    android:textSize="30sp"
    android:textStyle="bold"
    tools:layout_constraintTop_creator="1"
    tools:layout_constraintRight_creator="1"
    android:layout_marginStart="165dp"
    android:layout_marginEnd="165dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginTop="209dp"
    tools:layout_constraintLeft_creator="1"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

如果这有帮助,我在 Windows 7、Android Studio 2.3.2、Java 1.8 上,专门为 SM-T580(Samsung Tab A 10.1")设计应用程序,TextView 的父级是组件树的基本 ConstraintLayout

【问题讨论】:

  • 在设置文本后执行此操作 messageTextView.setText(messageTextView.replace("\\n", "\n"));
  • 你可以在这里为你的布局添加 XML 代码,其中包含 TextView
  • 你可以在这里发布 xml 代码
  • 这是管理文本视图(如填充)和边距属性以设置消息布局的最佳方式。
  • 不要使用emsinputType

标签: java android


【解决方案1】:

试试这个\n对应ASCII char 0xA,也就是'LF'或者换行

        tv.setText("First line " + System.getProperty("line.separator") + "Line 2"+ System.getProperty("line.separator") + "Line 3");


String String1 = "value 1";
String String2 = "value 2";
TextView.setText(String1 + "\n" + String2);

或试试这个

string = string.replace("\\\n", System.getProperty("line.separator"));

对于核心字符串试试这个

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

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

【讨论】:

  • \n 对我不起作用,System.getProperty("line.separator") 字符串必须以编程方式传入,因此我无法将字符串硬编码为 XML
  • @pianoisland 试试这个 tv.setText("First line" + System.getProperty("line.separator") + "Line 2"+ System.getProperty("line.separator") + "Line 3");
【解决方案2】:

android:inputType不需要设置,因为这是一个TextView。这些是我成功实施新线路的步骤:

  1. 确保 android:maxLine 未设置为 1。如果将其设置为 1,则不会执行换行。 [参考截图]

set maxLine inside xml file

  1. 如果您想操作一个不是常量或未在您的 string.xml 中声明的字符串,请将 System.getProperty("line.separator") 放在两个字符串之间。就我而言,我把

    phoneNumber = memberProfile.getPhoneNumber() + System.getProperty("line.separator") + "09162343636";

  2. 如果您有一个常量字符串,或者您已将其设置为您的 string.xml 文件,只需输入“\n”即可完美运行。请参考以下示例:

    string.xml

我使用了一个 recyclerView 来显示,所以这里是这个的输出:

Output

【讨论】:

    【解决方案3】:

    \r\n 适合我

    messageTextView.setText("First line\r\nNext line");
    

    或者你也可以使用字符串变量

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

    因此需要在 CDATA 中进行包装,并在其中添加中断作为 html 标记

    【讨论】:

    • 你能想出它对我不起作用的任何原因吗?会不会是我的环境有问题?
    • \r 不是必需的。 Android 是基于 Linux 的,\n 是换行符。
    • 您在哪个平台上使用 Android Studio? Windows 或 Linux
    • Windows,在我的问题中有说明
    • @JayPatel 首先,它的 \r 和 ]n,而不是正斜杠。其次,Android 是一个 linux 操作系统。这才是最重要的——你运行的平​​台,而不是你编译的平台。这意味着你只需要 linux 换行符,\n
    【解决方案4】:

    这对我有用:

    messageTextView.setText(Html.fromHtml("This<br/>Is<br/>A Test"));
    

    说明:TextView 内容为 HTML 内容。 &lt;br/&gt; 是用于换行的 HTML 标记。

    【讨论】:

      【解决方案5】:
      messageTextView.setText(Html.fromHtml("This\n Is\n A Test"));
      

      【讨论】:

        【解决方案6】:

        试试这个。它对我有用

        message = "This"+System.getProperty("line.separator") 
        + "Is" + System.getProperty("line.separator") + "A Test";
        // Next Activity
        TextView messageTextView = (TextView) findViewById(R.id.messageToUser);
        String message = getIntent().getStringExtra("message");
        messageTextView.setText(message);
        

        【讨论】:

        • 这与为 System.getProperty("line.separator") 初始化变量有何不同?
        • System.getProperty("line.separator") 将返回依赖于操作系统的行分隔符。
        • 这对我有用。我从我们的后端得到了我的文本字符串,让换行符工作的唯一方法是用 System.getProperty("line.separator") 替换。
        【解决方案7】:

        将此代码添加到 res/strings

        <string name="myhtml">
        <![CDATA[
        <p>This is a <b>bold</b> and <i>italic</i> text</p>
        <p>This is another paragraph of the same string.</p>
        ]]>
        </string>
        

        将此添加到您的 Activity 类

        TextView tv = (TextView)findViewById(R.id.foo);
        tv.setText(Html.fromHtml(getString(R.string.myhtml)));
        

        【讨论】:

          【解决方案8】:

          发现了我必须将android:inputType="textPersonName" 从 TextView 的 XML 中取出的问题。当我最初删除android:inputType="textPersonName" 时,我错误地离开了Html.fromHtml(...) 方法调用。在没有 android:inputType="textPersonName" 的情况下调用 message.setText("This\n Is\n A Test");。一切都按预期工作。

          为了最终清楚......

          如果输入类型是“textPersonName”,换行符“\n”将不起作用

          Andriod 的换行符是“\n”

          【讨论】:

          • 谢谢你,这解决了我的问题!
          【解决方案9】:

          试了很多次,终于成功了

          messageTextView.setText(string.replace("\n", ""+System.getProperty("line.separator")));

          【讨论】:

          • 欢迎来到 Stack Overflow。这个问题很老,并且已经得到了公认的答案以及许多其他(类似的)答案。 在添加另一个答案之前,请先问问自己,再添加一个迟到的答案有什么价值?它是否增加了一些新的东西?这是因为以前的答案已经过时或过时而导致的更新吗?如果您这么认为,那么您的答案应该可以更好地解释这一点。回答旧的(已经回答的)问题时要小心。
          猜你喜欢
          • 2014-07-19
          • 2021-10-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-08-18
          • 2016-09-22
          • 2016-02-22
          相关资源
          最近更新 更多