这是我要在 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<stdio.h>
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> </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 文件时显示如下:
请帮帮我。
最佳答案
Html.fromHtml()
将让您在 TextView
中查看轻量级 HTML
:
myTextView.setText(Html.fromHtml(htmlContentInStringFormat));
如果你想让你的 JS 和 CSS 使用它,你需要使用 WebView
。要将 Assets 中的 HTML
文件加载到 WebView
中,您可以 refer this answer here .
关于javascript - 我想在android中的TextView上显示html文件的内容(带格式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40570476/