I'd like to display a text with a linebreak in an Alert Message:
private void showAbout() {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
String message = "<b>Rechtlicher Hinweis:</b>\nLorem ipsum dolor sit amet, consectetur adipiscing elit. Sed a dolor sapien. Etiam arcu erat, lobortis sed vestibulum ut, adipiscing et enim. Nulla vestibulum volutpat dolor, non pellentesque purus imperdiet vitae. Aenean et elit vel erat consectetur pulvinar. Sed semper, ante vel elementum aliquet, dui urna scelerisque tortor, eu auctor lorem nunc adipiscing velit. Praesent eget libero diam, eget imperdiet sem. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae.\n" + getVersionInfo();
builder.setMessage开发者_开发知识库(Html.fromHtml(message));
builder.setCancelable(false);
builder.setPositiveButton("Close", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder.setTitle("About");
builder.setIcon(R.drawable.icon);
AlertDialog alert = builder.create();
alert.show();
}
However this works more or less fine, the linebreaks
\n
aren't displayed at all. I already tried to replace \n
by \\n
or even <\br>
but nothing worked. Any hints how to do this?
change
builder.setMessage(Html.fromHtml(message));
to
builder.setMessage(Html.fromHtml(message.replace("\n","<br />"));
Try <br />
(note the slash is after the tag name plus it's a forward slash, not a backward one).
The Html linebreak tag is <br/>
@Aaron Digulla it not work for me , but i find this: https://developer.android.com/reference/android/text/Html.html#FROM_HTML_MODE_LEGACY
replace "\n" with "\n\n"
it work for me.
精彩评论