【问题标题】:I want to display the contents of html file (with formatting) on TextView in android我想在 android 的 TextView 上显示 html 文件的内容(带格式)
【发布时间】:2016-11-13 04:10:34
【问题描述】:

这是我想在 Textview 上显示的 HTML 文件:

<html>
<head>
    <link rel="stylesheet" href="highlight/styles/default.css">
    <script src="highlight/highlight.pack.js"></script>
    <script>hljs.initHighlightingOnLoad();</script>
</head>
<body>

<h1>C Program to Calculate Area of Circle</h1>
<p class="test"><pre><code class="c">#include&lt;stdio.h&gt;

int main() {
   area = 3.14 * radius * radius;
   printf("\nArea of Circle : %f", area);
}
</code></pre></p>
<br>
<!--Output-->
<div><br>Enter the radius of Circle : 2.0<br>
Area of Circle : 6.14<br>&nbsp;</div>
</body>
</html> 

它有外部链接到一些 .js.css 文件

我已将其存储在 assets 文件夹中。

htmlContentInStringFormat="";
String htmlFilename = "www/"+fileName;
AssetManager mgr = getBaseContext().getAssets();
InputStream in = mgr.open(htmlFilename, AssetManager.ACCESS_BUFFER);
htmlContentInStringFormat = StreamToString(in);
in.close();

我正在使用 mTextview.setText(Html.fromHtml(htmlContentInStringFormat)),但它显示的是纯文本。我想要正确的风格化文本,当我像这样打开 .html 文件时会显示它:

desired output

请帮帮我。

【问题讨论】:

  • 为此使用 webview。
  • 目前我只使用 WebView,但在某些设备上加载似乎很慢。这就是为什么我想在 TextView 上显示它。

标签: javascript android html css


【解决方案1】:

Html.fromHtml() 将让您在TextView 中查看轻量级HTML

myTextView.setText(Html.fromHtml(htmlContentInStringFormat));

如果您希望您的 JS 和 CSS 使用它,您将需要使用 WebView。要将HTML 文件从您的资产加载到WebView,您可以refer this answer here

【讨论】:

  • 我只使用它,但我没有得到想要的输出。请查看我发布的带有问题的图片,这就是我希望文本出现在 TextView 上的方式,但它没有链接我用于设置 html 样式的外部 .js 和 .css 文件。
  • 如果您希望外部 JS 和 CSS 使用它,您需要将其加载到 WebView 中。 TextView 不能处理完整的网页。
猜你喜欢
  • 2015-06-13
  • 1970-01-01
  • 2015-11-02
  • 2014-01-05
  • 2014-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多