如何在AlertDialog框Android中以粗体和分隔线显示消息

时间:2016-12-23 12:02:12

标签: android alertdialog

我想在两个单独的行中显示AlertDialog的消息,并在粗体样式和下一行简单文本(没有任何样式)的一行中显示文字。

现在,如果我使用粗体文本执行断行,则会断行,但文本不显示粗体。为什么?任何人都可以帮助我。

代码:

AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
    alertDialog.setTitle("Alert");
    Spanned s1 = Html.fromHtml("<b>" + "Text in Bold"+ "</b>");
    String s2 = "Normal Text";
    alertDialog.setMessage(s1+ "\n" + s2);
AlertDialog dialog = alertDialog.create();
dialog.show();

2 个答案:

答案 0 :(得分:4)

请你试试这个:

AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
    alertDialog.setTitle("Alert");
    String s1 = "<b>" + "Text in Bold"+ "</b>";
    String s2 = "Normal Text";
    Spanned strMessage = Html.fromHtml(s1+  "<br>" + s2);
    alertDialog.setMessage(strMessage);
AlertDialog dialog = alertDialog.create();
dialog.show();

答案 1 :(得分:0)

使用\ n换行。

AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("Alert");
String s1 = "<b>" + "Text in Bold"+ "</b>";
String s2 = "Normal Text";
Spanned strMessage = Html.fromHtml(s1+  "\n" + s2);
alertDialog.setMessage(strMessage);
AlertDialog dialog = alertDialog.create();
dialog.show();