如何使用android将html内容显示到webview

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3624171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:02:23  来源:igfitidea点击:

how to show the html contents to the webview using android

htmlandroidandroid-webview

提问by Amit Thaper

Following is my html content which i want to show in the webview using android sdk. It will displays only

以下是我想使用 android sdk 在 webview 中显示的 html 内容。它只会显示

//Please

//请

But when I put this HTML content into the browser then it shows differently.

但是当我将此 HTML 内容放入浏览器时,它的显示方式有所不同。

<br /><br />Read the handouts please for tomorrow.<br /><br /><!--homework help homework


help help with homework homework assignments elementary school high school middle school



// --><font color="#60c000" size="4"><strong>Please!</strong></font>

Please suggest how to resolve this problem

请建议如何解决这个问题

I have another problem that in HTML content there is a tag

我有另一个问题,在 HTML 内容中有一个标签

<img src="http://www.homeworknow.com/hwnow/upload/images/tn_star300.gif" border="0" />

in this images does not shows.

在这张图片中没有显示。

回答by droidgren

  1. Use web.loadDataWithBaseURL instead of web.loadData (And don't forget to escape strings where it's needed)
  2. You need to add internet permission to download images and view them in your manifest file.
  1. 使用 web.loadDataWithBaseURL 而不是 web.loadData (并且不要忘记在需要的地方转义字符串)
  2. 您需要添加 Internet 权限才能下载图像并在清单文件中查看它们。

This example works for me:

这个例子对我有用:

public class SimpleMusicStream extends Activity {
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);

        WebView wv = (WebView) findViewById(R.id.WebView01);        

        final String mimeType = "text/html";
        final String encoding = "UTF-8";
        String html = "<br /><br />Read the handouts please for tomorrow.<br /><br /><!--homework help homework" +
                "help help with homework homework assignments elementary school high school middle school" +
                "// --><font color='#60c000' size='4'><strong>Please!</strong></font>" +
                "<img src='http://www.homeworknow.com/hwnow/upload/images/tn_star300.gif'  />";


        wv.loadDataWithBaseURL("", html, mimeType, encoding, "");
    }

}

And don't forget to add:

并且不要忘记添加:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

in your AndroidManifest.xml file

在您的 AndroidManifest.xml 文件中

回答by Android

Either you can do it as above or put html file into the asset folder and the use it loke this to display it

您可以按照上述方法进行操作,也可以将 html 文件放入资产文件夹中,然后使用它来显示它

view.loadUrl("file:///android_asset/FILENAME.html");

回答by Android

for image display you can do it like

对于图像显示,您可以这样做

 String str= " img src=\"http://www.homeworknow.com/hwnow/upload/images/tn_star300.gif\" alt=\"this is img\"ALIGN=\"right\"/>";
    wv.loadData(str, "text/html", "utf-8");