我有一些信息要显示在对话框中。它类似于Title,然后是text;Title,然后是text。像wise一样,有4个标题和4个描述要显示。它应该是这样的
Title One
description;description;description;description;description;description;description;description;description;description;description;description;description;description;description
Title Two
description;description;description;description;description;description;description;description;description;description;description;description;description;description;description
正如你所看到的,有粗体文本,带下划线的文本,换行符等。我想将这种文本添加到警告框中,所以下面是我尝试的内容。
TextView msg = new TextView(this);
msg.setText("<html><u>Message</u></html>")
AlertDialog.Builder ab = new AlertDialog.Builder(this);
ab.setTitle("Title");
ab.setView(msg);
ab.setCancelable(false);
//rest of the code
然而,这个技巧并不起作用。实际情况是,所有的HTML标记都是原样显示的!文本不清晰!看起来像是与AlertBox的默认颜色黑色的背景混合在一起。我该如何解决这个问题?请帮帮我!
PS:还是我用错了方法?错误的对话框?
发布于 2013-02-02 02:40:43
您需要在TextView
中使用Html.fromHtml()
标记,如下所示:
msg.setText(Html.fromHtml("<u>Message</u>"))
您还可以看到所有的HTML tags supported by TextView
。
发布于 2015-07-30 03:18:28
事实证明,您实际上不需要任何额外的TextViews来做到这一点。只需在警报的"setMessage()“调用(它替换了问题中的"setView()”调用)中包含HTML,并将html格式的字符串传递给它。但是,请确保在格式化中仅使用<b>
、<u>
和<i>
,因为它仅支持这些标记。但是,如果您对警报中的文本使用字符串资源,请调用getResources().getText(R.id.yourHtmlString)
而不是getResources().getString(R.id.yourHtmlString)
,否则标记将完全从字符串中剥离。
发布于 2013-02-02 12:19:10
如果要添加链接并使其可单击,
msg.setMovementMethod(LinkMovementMethod.getInstance());
msg.setClickable(true);
https://stackoverflow.com/questions/14652894
复制相似问题