Android 解决WebView调用loadData()方法显示乱码的问题

第一步:

mWebView.getSettings().setDefaultTextEncodingName("UTF-8"); 

第二步:

mWebView.loadData(data, "text/html; charset=UTF-8", null); 

WebView常用配置:

private void initWebView() {
  mWebView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
  mWebView.getSettings().setDefaultTextEncodingName("UTF-8");
  mWebView.getSettings().setJavaScriptEnabled(true);
  mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
  mWebView.getSettings().setSupportZoom(true);
  mWebView.getSettings().setDomStorageEnabled(true);
  mWebView.getSettings().setAllowFileAccess(true);
  mWebView.getSettings().setUseWideViewPort(true);
  mWebView.getSettings().setBuiltInZoomControls(true);
  mWebView.getSettings().setLoadWithOverviewMode(true);
  mWebView.requestFocus();
  mWebView.loadUrl(url);
  mWebView.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
      return super.shouldOverrideUrlLoading(view, url);
    }

    @Override
    public void onLoadResource(WebView view, String url) {
      super.onLoadResource(view, url);
    }

    @Override
    public void onPageStarted(WebView view, String url,
        android.graphics.Bitmap favicon) {

    };

    @Override
    public void onPageFinished(WebView view, String url) {
      super.onPageFinished(view, url);
    }
  });

}

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

02-04 17:50
查看更多