简体   繁体   English

我想在android中的TextView上显示html文件的内容(带格式)

[英]I want to display the contents of html file (with formatting) on TextView in android

This is the HTML file I want to display on the Textview :这是我想在 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> 

It has external linking to some .js and .css files它有一些.js.css文件的外部链接

I've stored it in assets folder.我已经将它存储在资产文件夹中。

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

I'm using mTextview.setText(Html.fromHtml(htmlContentInStringFormat)) , but it is displaying plain text.我正在使用mTextview.setText(Html.fromHtml(htmlContentInStringFormat)) ,但它显示纯文本。 I want proper stylized text, as it is displayed when I open the .html file like this :我想要适当的风格化文本,因为它在我打开.html文件时显示如下:

desired output期望的输出

Please help me.请帮帮我。

Html.fromHtml() will let you view light weight HTML in a TextView : Html.fromHtml()会让你在TextView查看轻量级的HTML

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

If you want your JS and CSS to work with it, you will need to use a WebView .如果你想让你的 JS 和 CSS 使用它,你将需要使用WebView To load an HTML file from your assets into a WebView you may refer this answer here .要将您的资产中的HTML文件加载到WebView您可以在此处参考此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM