Android textview not supporting line break

后端 未结 7 1681
遥遥无期
遥遥无期 2020-12-31 02:30

I\'m creating a custom view programmatically that is displaying text that is parsed from an XML file. The text is long and contains the \"/n\" character for force line brea

7条回答
  •  情书的邮戳
    2020-12-31 03:07

    To force a line break through the XML of your textview, you need to use \r\n instead of just \n.

    So, now your code in the XML becomes

    android:text="Now is the time \r\n for all good men to \r\n come to the aid of their \r\n party"
    

    Or if you want to do it programatically, then in your java code :

    fireBody.setText("Now is the time \r\n for all good men to \r\n come to the aid of their \r\n party");
    

    You can also declare the text as a string resource value, like this :

    "some test line 1 \n some test line 2"
    

    Another easy way to do it would be to change the default attribute of the TextView in your xml file.

    
    

    And then use, textView.setText("First line \nSecond line \nThird line");

提交回复
热议问题