简体   繁体   中英

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 :

<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 and files文件的外部链接

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 , but it is displaying plain text. ,但它显示纯文本。 I want proper stylized text, as it is displayed when I open the file like this :文件时显示如下:

desired output

Please help me.

Html.fromHtml() will let you view light weight HTML in a TextView :

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

If you want your JS and CSS to work with it, you will need to use a WebView . To load an HTML file from your assets into a WebView you may refer this answer here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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