这里我先简单描述一下需求:服务器返回的是html页面的一部分带有标签的内容。解决的思路是:将服务器返回的内容片段拼凑成一个完整的页面。下面直接上核心代码:这里是WEBView一些配置WebSettings settings = webVie
这里我先简单描述一下需求:服务器返回的是html页面的一部分带有标签的内容。
解决的思路是:将服务器返回的内容片段拼凑成一个完整的页面。
下面直接上核心代码:
这里是WEBView一些配置
WebSettings settings = webView.getSettings();settings.setjavascriptEnabled(true);settings.setDomStorageEnabled(true);settings.setUseWideViewPort(true);settings.setLoadWithOverviewMode(true);webView.setWebViewClient(new MyWebViewClient(activity));if (Build.VERSioN.SDK_INT >= Build.VERSION_CODES.KITKAT) { settings.setLayoutAlGorithm(WebSettings.LayoutAlgorithm.TEXT_AUTOSIZING);} else { settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);}webView.loadData(getHtmlData(item.content), "text/html;charset=utf-8","utf-8");private String getHtmlData(String bodyHTML) { String head = "<head>" + "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-Scalable=no\"> " + "<style>html{padding:15px;} body{Word-wrap:break-word;font-size:13px;padding:0px;margin:0px} p{padding:0px;margin:0px;font-size:13px;color:#222222;line-height:1.3;} img{padding:0px,margin:0px;max-width:100%; width:auto; height:auto;}</style>" + "</head>"; return "<html>" + head + "<body>" + bodyHTML + "</body></html>"; }static class MyWebViewClient extends WebViewClient{ private WaitingDialog dialog; private Activity activity; public MyWebViewClient(Activity activity){ dialog = new WaitingDialog(activity); this.activity = activity; } @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { LogUtil.e(url); view.loadUrl(url); return true; } @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); if(!activity.isFinishing()) dialog.show(); } @Override public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { handler.proceed(); super.onReceivedSslError(view, handler, error); } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); if(!activity.isFinishing()) dialog.dismiss(); }
--结束END--
本文标题: 详解Android WebView加载html片段
本文链接: https://lsjlt.com/news/221359.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0