android html 容器
有谁知道是否可以在android设备上显示带有html代码的对话框?我将非常感谢任何有关它的提示。
我想要的是设计一个 html 布局并将其显示在自定义应用程序内的对话框中。
谢谢!
does anyone know whether is it possible to display a dialog with html code on android device? I would be more than thankful for any tip about it.
What I want to di is design a html layout and display it in a dialog inside the custom application.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
WebView 绝对是一个选择,因为它本质上是一个盒子里的 Web 浏览器,但正如 Mark 所说,它有点重量级,会导致明显的延迟。
TextView 也可以处理基本的 HTML,如果您只是寻找一些简单的格式(粗体、斜体、颜色),那么使用标准文本视图是不错的选择。
了解 String 和 CharSequence(和 Spannable)之间的区别很重要 - 字符串没有 HTML 支持,如果您从资源中获取字符串(通过 Context.getString()),它将自动删除所有 HTML 代码。所以你需要使用Context.getText())。
无论如何,这里有一个例子:
WebView is definitely an option since it is essentially a web browser in a box, but like Mark said, it's a bit heavyweight and will cause a noticeable delay.
TextViews can handle basic HTML too, and if you're just looking for some simple formatting (bold, italics, color), then using the standard text view is the way to go.
It's important to know the difference between a String and a CharSequence (and Spannable) - Strings do not have HTML support, and if you grab a string from the resources (through Context.getString()), it will automatically strip out all HTML code. So you need to use Context.getText()).
In any case, here is an example:
扩展 JRL 的答案,您可以使用
AlertDialog.Builder
及其setView()
将WebView
放置在对话框的主要区域中。但请注意,如果您没有在应用程序中的其他任何地方使用WebView
,则第一次使用它时,将需要一秒钟左右的时间来初始化,这可能会使您的对话框变得缓慢。如果您需要的只是基本的 HTML 格式(粗体、斜体、颜色),
TextView
采用Spannable
,Android 中的Html
类可以转换从 HTML 源到Spannable
以便与TextView
一起使用。Expanding upon JRL's answer, you can use
AlertDialog.Builder
and itssetView()
to put aWebView
in the main area of the dialog. Note, though, that if you are not usingWebView
anywhere else in your application, the first time you use it, it will take a second or so to initialize, which may make your dialog sluggish.If all you need is rudimentary HTML formatting (bold, italics, color),
TextView
takes aSpannable
, and theHtml
class in Android can convert from HTML source to aSpannable
for use withTextView
.不确定您要做什么,但是
WebView
处理 HTML。Not sure what you're trying to do, but
WebView
handles HTML.