AlertDialog message with new line. (\n doesn't work.)

纵饮孤独 提交于 2019-12-10 16:35:17

问题


I need to use a break-line in a AlertDialog on Android. I already tried with the "\n" character but it doesn't work. I don't see the "\n" character but the text wont wrap.

Any help?


回答1:


You can use Spanned text using Html.fromHtml("some string" + "<br>") to create new line on some textview element.




回答2:


If you are receiving the String from somewhere, maybe the \n is being escaped.

Try with:

string = string.replace("\\n","\n");



回答3:


How about using the HTML tag to set new text?

    controlName.setText(Html.fromHtml(outputString + "<br/>" + outputString2));



回答4:


It doesn't matter when use "\n" in Dialog or Activity/Fragment.
I faced the same issue but it turned out the TextView have the attribute: android:singleLine="true" or android:maxLines="1", it conflicts with "\n" so make sure to REMOVE these attributes first.



来源:https://stackoverflow.com/questions/24166315/alertdialog-message-with-new-line-n-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!