[英]I want to display the contents of html file (with formatting) on TextView in android
这是我想在 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文件的外部链接
我已经将它存储在资产文件夹中。
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
。 要将您的资产中的HTML
文件加载到WebView
您可以在此处参考此答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.