Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想通过代码为 textView 设置文本。我的文字是这样的:“aaaa \n bbb\n ccc”,如果我在屏幕上使用 setText() 方法,我得到的格式是准确的,而不是这样:
"aaaa bbb ccc".
如果我输入 xml android:text="aaaa \n bbb\n ccc" 它工作正常。我需要通过代码设置它,而不是从 xml 文件。我怎样才能做到这一点?
使用 HTML 标签:
myTv.setText(Html.fromHtml("<p>aaaa<br/>bbbb<br/>cccc</p>"));
只需使用换行符“\n”,如下所示:
myTextView.setText("aaaa\nbbb\nccc");
这将输出:
aaaa bbb ccc
试试这样:
setText("aaaa\nbbb\nccc");