详解Android WebView加载html片段
作者:李moumou 时间:2023-04-23 11:40:12
这里我先简单描述一下需求:服务器返回的是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();
}
笔者在Nexus6 7.0上面测试偶尔出现,html内容无法加载出来,显示一片空白,开启硬件加速后完美解决。
在上面WebView设置的部分加上如下代码
if (webView.isHardwareAccelerated()) settings.setJavaScriptEnabled(true);
怎么样简单吧,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
来源:http://www.jianshu.com/p/29ecf8c2c454?utm_source=tuicool&utm_medium=referral
标签:Android,WebView,加载html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java 制作验证码并进行验证实例详解
2023-03-18 11:27:56
Android动画之雷达扫描效果
2021-06-10 08:06:52
![](https://img.aspxhome.com/file/2023/0/137900_0s.gif)
QR 二维码中插入图片实现方法
2023-01-24 14:32:32
![](https://img.aspxhome.com/file/2023/1/76511_0s.png)
C#中WebBroeser控件用法实例教程
2021-09-14 18:11:05
android长截屏原理及实现代码
2021-11-13 05:55:50
![](https://img.aspxhome.com/file/2023/4/128984_0s.jpg)
Java类成员访问权限控制知识总结
2021-09-12 10:36:53
![](https://img.aspxhome.com/file/2023/5/96075_0s.png)
Android仿百度图片查看功能
2023-09-26 07:50:24
![](https://img.aspxhome.com/file/2023/8/85448_0s.jpg)
OpenCV和C++实现图像的翻转(镜像)、平移、旋转、仿射与透视变换
2023-07-14 23:47:22
![](https://img.aspxhome.com/file/2023/3/95523_0s.jpg)
C#检测远程计算机端口是否打开的方法
2023-12-11 08:55:31
springboot下mybatis-plus如何打印sql日志和参数到日志文件
2023-06-15 12:34:05
![](https://img.aspxhome.com/file/2023/3/84213_0s.png)
JAVA中阻止类的继承(官方和非官方)
2023-06-29 16:02:56
解决C#获取鼠标相对当前窗口坐标的实现方法
2022-10-07 12:57:57
详解Java如何创建Annotation
2023-05-05 22:40:41
![](https://img.aspxhome.com/file/2023/1/108451_0s.jpg)
C#线程入门教程之单线程介绍
2022-03-15 20:37:28
![](https://img.aspxhome.com/file/2023/9/69819_0s.jpg)
Android巧用ViewPager实现左右循环滑动图片
2021-07-13 16:32:32
![](https://img.aspxhome.com/file/2023/4/118974_0s.gif)
SpringBoot中使用Session共享实现分布式部署的示例代码
2022-10-17 04:27:54
![](https://img.aspxhome.com/file/2023/7/66687_0s.png)
HTTP中get和post的区别详解
2023-04-19 11:42:18
SpringBoot2.x 集成 Thymeleaf的详细教程
2021-07-31 18:18:23
![](https://img.aspxhome.com/file/2023/1/114041_0s.png)
springboot 自定义异常并捕获异常返给前端的实现代码
2022-07-23 03:09:52
![](https://img.aspxhome.com/file/2023/5/71625_0s.png)
深入解析Java的设计模式编程中建造者模式的运用
2022-08-10 23:31:22
![](https://img.aspxhome.com/file/2023/5/76315_0s.jpg)