我想在警报消息中显示带有换行符的文本:
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();
}
然而,这或多或少都可以正常使用,
\ n
根本不显示。我已经尝试将\n
替换为\\n
甚至<\br>
,但没有任何效果。任何提示如何做到这一点?
答案 0 :(得分:28)
更改
builder.setMessage(Html.fromHtml(message));
到
builder.setMessage(Html.fromHtml(message.replace("\n","<br />"));
答案 1 :(得分:25)
尝试<br />
(注意在标记名称之后斜杠是加上它是正斜杠,而不是向后斜杠。)
答案 2 :(得分:2)
Html换行标记为<br/>
答案 3 :(得分:0)
替换&#34; \ n&#34;用&#34; \ n \ n&#34;
它适合我。