今晚在学一首歌......真jier难唱......感谢邻居们没来砸我家大门。
Webview的一些属性
webView.loadUrl("www.baidu.com");//WebView加载的网页使用loadUrl
WebSettings webSettings = webView.getSettings();//获得WebView的设置
webSettings.setUseWideViewPort(true);// 设置此属性,可任意比例缩放
webSettings.setLoadWithOverviewMode(true);//适配
webSettings.setJavaScriptEnabled(true); //支持js
webSettings.setCacheMode(WebSettings.LOAD_DEFAULT); //设置 缓存模式
webSettings.setDomStorageEnabled(true);// 开启 DOM storage API 功能
webSettings.setDatabaseEnabled(true);//开启 database storage API 功能
webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);//HTTPS,注意这个是在LOLLIPOP以上才调用的
webSettings.setAppCacheEnabled(true);//开启 Application Caches 功能
webSettings.setBlockNetworkImage(true);//关闭加载网络图片,在一开始加载的时候可以设置为true,当加载完网页的时候再设置为false
webview的监听
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
//加载的进度
}
@Override
public void onReceivedTitle(WebView view, String title) {
//获取WebView的标题
}
@Override
public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
return super.onJsAlert(view, url, message, result);
//Js 弹框
}
@Override
public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
AlertDialog.Builder b = new AlertDialog.Builder(IllegalQueryActivity.this);
b.setTitle("删除");
b.setMessage(message);
b.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
result.confirm();
}
});
b.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
result.cancel();
}
});
b.create().show();
return true;
}
});
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//需要设置在当前WebView中显示网页,才不会跳到默认的浏览器进行显示
return true;
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
//加载出错了
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
//加载完成
}
});
webView.setDownloadListener(new DownLoadListener());//下载监听
private class DownLoadListener implements DownloadListener {
@Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
}
}
注意需要及时销毁webview否则会内存泄漏
@Override
protected void onDestroy() {
if (webView != null) {
webView.clearCache(true); //清空缓存
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
if (webViewLayout != null) {
webViewLayout.removeView(webView);
}
webView.removeAllViews();
webView.destroy();
}else {
webView.removeAllViews();
webView.destroy();
if (webViewLayout != null) {
webViewLayout.removeView(webView);
}
}
webView = null;
}
}
判断版本是因为如果低于5.0版本的WebView中,如果先在parent中remove了WebView,那WebView将无法进行destroy了,这样就会造成内存的泄漏。
Over 一刀两断 如意神剑
光能使者.jpg
网友评论