您的位置:首页 > 移动开发

Android 网页展示之WebView

2015-12-08 23:14 417 查看

使用场景

在应用的开发过程中,经常遇到需要经常变化的页面,一般针对这种页面,我们会用WebView来实现.如隐私条款等等.

注意事项

为了我们的WebView能够支持Js ,需要设置

webview.getSettings().setJavaScriptEnabled(true);


loadData使用
#、%、\、?
四种字符易出现问题.

%:会报找不到页面错误,页面全是乱码。乱码样式见符件。

#:会让你的goBack失效,但canGoBAck是可以使用的。于是就会产生返回按钮生效,但不能返回的情况。

\ :会被当做转义字符.

如果出现乱码问题,encoding参数传”utf-8”即可.

mimeType,指定mineType类型,如”text/html; charset=utf-8”

loadDataWithBaseURL

WebView加载页面的方式

public void loadDataWithBaseURL(String baseUrl, String data,
String mimeType, String encoding, String failUrl) {
}
public void loadData(String data, String mimeType, String encoding) {
}
public void loadUrl(String url) {
}
public void loadUrl(String url,Map<Stirng,String> extraHeaders) {
}


loadDataWithBaseURL():可以指定相对根路径,也可以指定历史Url。

baseUrl:相对根路径,常见于WebView中的超链接或者是image元素,如使用绝对路径传”“即可。

ps: 我们的data格式:

String body ="示例:这里有个img标签,地址是相对路径<img src='/uploads/allimg/130923/1FP02V7-0.png' />";


如果baseUrl没有指定为网站域名,那么这张图片将显示不出来。

常用属性

通过mWebView.getSettings()拿到WebView的设置.

mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);//支持通过JS打开新窗口
mWebView.getSettings().setSupportZoom(true);//设置是否支持缩放
mWebView.getSettings().setBuiltInZoomControls(true);//设置是否显示缩放工具
webview.getSettings().setDisplayZoomControls(false);//设定缩放控件隐藏
/**http://stackoverflow.com/questions/5448841/what-do-setusewideviewport-and-setloadwithoverviewmode-precisely-do*/
webview.getSettings().setLoadWithOverviewMode(true);// loads the WebView completely zoomed out
webview.getSettings().setUseWideViewPort(true); //makes the Webview have a normal viewport (such as a normal desktop browser), while when false the webview will have a viewport constrained to its own dimensions (so if the webview is 50px*50px the viewport will be the same size)
webview.getSettings().setAllowFileAccess(true); // 允许访问文件

mWebView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);//一般很少会用到这个,用WebView组件显示普通网页时一般会出现横向滚动条,这样会导致页面查看起来非常不方便。//LayoutAlgorithm是一个枚举,用来控制html的布局,总共有三种类型://NORMAL:正常显示,没有渲染变化。//SINGLE_COLUMN:把所有内容放到WebView组件等宽的一列中。//NARROW_COLUMNS:可能的话,使所有列的宽度不超过屏幕宽度。
mWebView.getSettings().setDefaultFontSize(18);//设置默认的字体大小,默认为16,有效值区间在1-72之间。


//若要考虑兼容3.0以下版本则:
/**http://jingyan.baidu.com/article/48b37f8d0b918e1a646488ec.html*/
if(android.os.Build.VERSION.SDK_INT>=11){

this.getSettings().setDisplayZoomControls(false);

}else{

this.setZoomControlHide(this);

}

//Android 3.0(11) 以下使用以下方法:

//利用java的反射机制

public void setZoomControlHide(View view) {

try {

Class webview = Class.forName("android.webkit.WebView");

Method method = webview.getMethod("getZoomButtonsController");

zoomController = (ZoomButtonsController) method.invoke(this, null);

} catch (Exception e) {

e.printStackTrace();

}

}  


更多设置参考:www.cnblogs.com/zgz345/p/3768174.html

WebView 链接在WebView内部打开,而非调用系统浏览器的方法.

重写shouldOverrideUrlLoading,并且return true;

WebViewClient与WebChromeClient

WebViewClient主要帮助WebView处理各种通知、请求事件的,比如:

onLoadResource
onPageStart
onPageFinish
onReceiveError
onReceivedHttpAuthRequest


WebChromeClient主要辅助WebView处理Javascript的对话框、网站图标、网站title、加载进度等,比如:

onCloseWindow//关闭WebView
onCreateWindow()
onJsAlert //WebView上alert无效,需要定制WebChromeClient处理弹出
onJsPrompt
onJsConfirm
onProgressChanged
onReceivedIcon
onReceivedTitle//获取title


如果你的WebView只是用来处理一些html的页面内容,只用WebViewClient就行了,如果需要更丰富的处理效果,比如JS、进度条等,就要用到WebChromeClient

onPageFinished:

onReceivedTitle()方法在goback()之后无效。

这时,我们可以使用
onPageFinished(WebView view, String url)
方法来实现返回及时刷新标题

WebView mWebView = (WebView) findViewById(R.id.mwebview);
mWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
setTitle(view.getTitle());
}
});


处理https请求

android 默认是不处理https请求的,请求https会显示空白。

webView.setWebViewClient(new WebViewClient() {

@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
//default
//            handler.cancel();
//            handler.handleMessage(null);
}
});


cookies 和历史纪录

//cookies清理
CookieSyncManager.createInstance(this);
CookieSyncManager.getInstance().startSync();
CookieManager.getInstance().removeSessionCookie();

//清理cache 和历史记录
webView.clearCache(true);
webView.clearHistory();


参考:泡在网上的日子

Android WebView中获取网页的title (包括调用goback)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: