WebView vs TextView for displaying html content

為{幸葍}努か 提交于 2020-01-25 19:58:33

问题


I have a number of html pages as Strings in my android app. So you might visualize it as List<String> myWebPages where each webpage is an html page equipped with css and javascript and html body. What is the best way to display these web pages to user? Should I use WebView or should I use a TextView? Notice that I don't have urls for the WebView; how would I pass the String? As for the TextView, can it handle full web pages with css and javascript?

Update

When I use

webView.loadData(Html.fromHtml(value).toString(), "text/html", "UTF-8");

my code is a pile of non-sense; it does not interpret the css etc well apparently.

For completeness, here is my html code (template in any case)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <link href="/stylesheets/shCore.css" rel="stylesheet" type="text/css"/>
    <link href="/js/style/shThemeDefault.css" rel="stylesheet" type="text/css"/>
    <script src="/js/shCore.js" type="text/javascript"></script>
    <script src="/js/shAutoloader.js" type="text/javascript"></script>
    <script type="text/javascript" src="/js/shBrushPython.js"></script>
    <script type="text/javascript" src="/js/shBrushJava.js"></script>
    <script type="text/javascript">
    SyntaxHighlighter.all();



    </script>
    <link rel="stylesheet" href="/stylesheets/jquery-ui.css"/>
    <link rel="shortcut icon" href="/img/favicon.ico" type="image/x-icon"/>
    <script src="/js/jquery/jquery-1.8.3.js"></script>
    <script src="/js/jquery/jquery-ui.js"></script>

    <style>

        .syntaxhighlighter .toolbar {
        background-color: #ffffff !important;
        color: #ffffff !important;
        }

        .syntaxhighlighter .toolbar a,.syntaxhighlighter .toolbar a:hover {
        background-color: #ffffff !important;
        color: #ffffff !important;
        }

    </style>
</head>

<body>

<pre class='brush:java'>my java code in here </pre>

</body>
</html>

By the way, I am not a web developer so the html is my best effort (if you find mistakes)


回答1:


Should I use WebView or should I use a TextView?

Since TextView has no support for CSS or JavaScript, you have no choice but to use WebView.

Notice that I don't have urls for the WebView; how would I pass the String?

Call loadData() on the WebView, passing the HTML.



来源:https://stackoverflow.com/questions/37007443/webview-vs-textview-for-displaying-html-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!