WebView 出现的若干问题记录

1.ScollView 嵌套 WebView  ,WebView加载富文本时,高度无法自适应解决,底部有时候会出现大量空白

解决方法:

WebView webView = new WebView(this);
webView.setWebViewClient(new SimpleWebViewClient(title));
        webView.getSettings().setDefaultTextEncodingName("utf-8");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.TEXT_AUTOSIZING);
} else {
     webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
}
webView.loadData(getHtmlData(bodyHTML), "text/html; charset=utf-8", "utf-8");

/**
 * 获取整个html
 * @param bodyHTML
 * @return
 */
private String getHtmlData(String bodyHTML) {

    String content ="\n" + "\n" + "\n" + "    title\n" + "    " + "\n" + "   " + " \n" + "\n" + " \n" + " "
            +bodyHTML + "\n" + "";
    return content;
}

2. Activiy 嵌套Webview, Webview 和 Android 交互 

案例:H5 调用Android 原生分享,使用Popupwindow 背景变暗无效

解决:放在主线程处理Ui

    @JavascriptInterface
        public void share(String data) {

            Message message = Message.obtain();
            message.obj = data;
            handler.sendMessage(message);

        }


private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            String data = (String) msg.obj;
            String[] split = data.split(",");
            if (split.length < 4) return;
            if (split[3].equals("0")) {
                split[3] = "";
            }
            showPopup(split[0], split[1], split[2], split[3]);

        }
    };

 

 

 

 

你可能感兴趣的:(【Android常见问题】)