WebView技巧

  • WebView技巧

    WebView启用相关配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
       wv = (WebView) findViewById(R.id.article_webwiew);
    wv.getSettings().setSupportMultipleWindows(true);
    wv.getSettings().setJavaScriptEnabled(true);
    wv.setWebViewClient(new WebViewClient());
    wv.setWebChromeClient(new WebChromeClient());
    wv.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);//关闭缓存
    wv.getSettings().setAllowFileAccess(true);//允许读取文件
    wv.getSettings().setAllowContentAccess(true);
    wv.getSettings().setSupportZoom(false);//禁止缩放
    wv.getSettings().setDomStorageEnabled(true);//开启本地数据库储存
    wv.getSettings().setDatabaseEnabled(true);

WebView 缩放

1
2
3
4
wv.getSettings().setSupportZoom(true);//禁止缩放
wv.getSettings().setBuiltInZoomControls(true);
wv.getSettings().setDisplayZoomControls(true);
wv.setInitialScale(100); //缩放比例

WebView JSON数据替换为String

1
2
3
4
5
6
7
8
9
webView = (WebView) findViewById(R.id.bbs_webview);  
webView.getSettings().setJavaScriptEnabled(true);
special_content = special_content.replaceAll("&", "");
special_content = special_content.replaceAll(""", "\"");
special_content = special_content.replaceAll("<", "<");
special_content = special_content.replaceAll(">", ">");
special_content = special_content.replaceAll("\\n", "<br>");//换行
special_content = special_content.replaceAll("<img", "<img width=\"100%\"");//图片不超出屏幕
webView.loadDataWithBaseURL(null, special_content, "text/html", "utf-8", null); //加载html源码