[英]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.