eclipse 在 Android 警报对话框中使用 HTML
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14652894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using HTML in Android Alert Dialog
提问by Lemon Juice
I have some amount of informations to be displayed in Dialog Box. It comes like Title, then under it text; Title, then under it text. Like wise, there are 4 titles and 4 descriptions to be displayed. It should come like this
我有一些信息要显示在对话框中。它像标题一样,然后在它下面是文本;标题,然后在它下面的文字。同样,有 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
描述;描述;描述;描述;描述;描述;描述;描述;描述;描述;描述;描述;描述;描述;描述;描述
As you can see, there are bold texts, underlined texts, line breaks etc. I want to add this kind of a text to the alert box, so below is what I tried.
如您所见,有粗体文本、带下划线的文本、换行符等。我想将这种文本添加到警报框中,下面是我尝试的内容。
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
However this trick didn't work. What happened is, all the HTML tags showed up as they are! And the text is not clear! Seems like it mixed with the background of the default colour of AlertBox, black. How can I solve this issue? Please help!
然而这个技巧并没有奏效。发生的事情是,所有的 HTML 标签都按原样显示了!而且文字不清楚!好像和AlertBox的默认颜色黑色的背景混在一起了。我该如何解决这个问题?请帮忙!
PS: Or am I using the wrong method? Wrong dialog box?
PS:还是我用错了方法?错误的对话框?
回答by ρяσ?ρ?я K
You will need to use Html.fromHtml()
to use HTML tags in TextView
as:
您将需要Html.fromHtml()
使用 HTML 标签TextView
作为:
msg.setText(Html.fromHtml("<u>Message</u>"))
And you also see all HTML tags supported by TextView
.
您还可以看到.html 支持的TextView
所有HTML 标签。
回答by alan
As it turns out, you don't actually need any extra TextViews to do this. Simply include the HTML in your alert's "setMessage()" call (which replaces the "setView()" call in your question) and pass it the html-formatted string. Be sure to only use <b>
, <u>
, and <i>
in your formatting, though because those are the only tags it supports. If you're using a String resource for the text in your alert, call getResources().getText(R.id.yourHtmlString)
rather than getResources().getString(R.id.yourHtmlString)
, though, or the tags will be completely stripped from the String.
事实证明,您实际上不需要任何额外的 TextView 来执行此操作。只需将 HTML 包含在警报的“setMessage()”调用中(它替换了问题中的“setView()”调用)并将 html 格式的字符串传递给它。请确保仅在您的格式中使用<b>
、<u>
和<i>
,因为它们是它支持的唯一标签。如果您对警报中的文本使用字符串资源,请调用getResources().getText(R.id.yourHtmlString)
而不是getResources().getString(R.id.yourHtmlString)
,否则标签将从字符串中完全剥离。
回答by YulCheney
If you want to add a link and make it clickable,
如果您想添加链接并使其可点击,
msg.setMovementMethod(LinkMovementMethod.getInstance());
msg.setClickable(true);
回答by keybee
If you need to add more complex HTML
, with CSS
and META
, you can add a WebView
to the dialog, like this:
如果您需要添加更复杂的HTML
,CSS
和META
,您可以WebView
在对话框中添加 a ,如下所示:
String webViewString = yourMeta + yourCss + yourHtml;
yourCustomWebView.loadData(webViewString, "text/html; charset=UTF-8",
null);
yourAlertDialog.setView(yourCustomWebView);
This way, you can display fully formatted HTML pages in your dialog.
这样,您就可以在对话框中显示完全格式化的 HTML 页面。
回答by MuraliGanesan
Try this, Font color,
试试这个,字体颜色,
String source = "<b><font color=#ff0000> Loading. Please wait..."
+ "</font></b>";
Font underline,
字体下划线,
String source = <u>Message</u>
msg.setText(Html.fromHtml(source));
回答by Bellan
In case if you need it.
Better to use HtmlCompat.fromHtml((htmlString, 0)
for compatibility with older versions.
如果你需要的话。更好地HtmlCompat.fromHtml((htmlString, 0)
用于与旧版本的兼容性。