Web视图如何加载Html内容?

时间:2013-04-04 07:35:53

标签: android android-webview

我通过变量String mresult=

在Response中获取此html内容
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Lorem Ipsum</title>
</head>
<body style="width:300px; color: #00000; ">

<p><strong> About us</strong> </p>
<p><strong> Lorem Ipsum</strong> is simply dummy text .</p>

<p><strong> Lorem Ipsum</strong> is simply dummy text </p>

<p><strong> Lorem Ipsum</strong> is simply dummy text </p>

</body></html>

无法加载webview,我必须尝试加载它:

web.loadDataWithBaseURL(null, mresult, "text/html", "UTF-8", null);

仍然webview显示空白页面。

xml文件中:

<WebView
        android:id="@+id/wV"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"          
        android:layout_margin="10dp"
        android:background="@android:color/transparent"
        android:cacheColorHint="#00000000" />

在活动中:

String mresult="Here my given data";
WebView web = (WebView)findViewById(R.id.wV);
web.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
});
web.loadData(mresult, "text/html; charset=UTF-8", null);

5 个答案:

答案 0 :(得分:24)

以这种方式使用此Html_value:

String html_value = "<html xmlns=\"http://www.w3.org/1999/xhtml\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\"><title>Lorem Ipsum</title></head><body style=\"width:300px; color: #00000; \"><p><strong> About us</strong> </p><p><strong> Lorem Ipsum</strong> is simply dummy text .</p><p><strong> Lorem Ipsum</strong> is simply dummy text </p><p><strong> Lorem Ipsum</strong> is simply dummy text </p></body></html>";

这用于webview,然后只有它才能正常工作,因为你的html标签没有正确关闭:

WebView browser = (WebView) findViewById(R.id.sample);
        browser.getSettings().setJavaScriptEnabled(true);
        browser.loadData(html_value, "text/html", "UTF-8");

输出:

enter image description here

答案 1 :(得分:3)

    wb.loadDataWithBaseURL("", result, "text/html", "UTF-8", "");

答案 2 :(得分:2)

尝试使用WebView的代码:

WebView wv = (WebView) findViewById(R.id.webView);
wv.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
});
wv.loadData(mresult, "text/html; charset=UTF-8", null);

答案 3 :(得分:2)

不知道是否仍未在webview中加载,但结果显示为TextView Html格式,如

TextView  txtweb = (TextView) findViewById(R.id.txtweb);
txtweb.setText(Html.fromHtml(mresult));

答案 4 :(得分:0)

使用此:

  webView.loadDataWithBaseURL("file:///android_asset/", htmlParsing, "text/html", "utf-8", null);
  webView.getSettings().setAllowFileAccess(true);

在htmlparsing中:只传递你的HTML代码。

它有效......