WebView的基本常用属性 及方法

来源:互联网 发布:大芒果魔兽数据库 编辑:程序博客网 时间:2024/05/18 06:41
一、在xml布局文件中定义WebView标签
<WebView
android:id="@+id/wv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
二、在activity中定义webview的属性及相应的函数
WebView wv=(WebView)findViewById(R.id.wv);
常用属性:
//设置webview的背景色
wv.setBackgroundColor(Color.GRAY);
//设置webview的字符集编码
wv.getSettings().setDefaultTextEncodingName("utf-8");
//设置webview的缩放功能 设置webview
wv.getSettings().setSupportZoom(true);
wv.getSettings().setBuiltInZoomControls(true);
//设置webview中滚动条的样式 INSIDE_OVERLAY INSIDE_INSET OUTSIDE_INSET OUTSIDE_OVERLAY
wv.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_INSET);
//允许水平滚动条出现,false禁止水平滚动条
wv.setHorizontalScrollBarEnabled(true);
//设置双击webview时变大,再次双击时变小 当手动放大是 双击可以恢复到原始大小
wv.getSettings().setUseWideViewPort(true);
//设置webview 的初始化尺寸 参数为百分比 100表示按原大小的百分百展示
wv.setInitialScale(100);
//允许js执行 如果访问的html页面中存在JavaScript脚本语言 webview中需要设置支持
wv.getSettings().setJavaScriptEnabled(true);

WebView中常用加载不同类型数据的方法:
用法1: loadUrl()该方法直接加载网络地址 wv.loadUrl("http://china.huanqiu.com/article/2016-09/9405086.html");
用法2:loadData() 加载html的字符串
/*loadData(String data, String mimeType, String encoding)
String data, 表示当前加载html字符串的内容 加载html字符串中 不能出现英文字符 # % ?
String mimeType, data的数据类型 text/html
String encoding data数据的编码格式 data数据出现中文乱码 指定编码utf-8
*/
(1)wv.loadData(content,"text/html;charset=uft-8",null);
或者是:(2)wv.loadData(content,"text/html","utf-8");
用法3:加载指定的url的网页内容 在assets资源文件中的数据
wv.loadUrl("file:///android_asset/aboutus.html");
用法4:
wv.loadDataWithBaseURL("http://www.baidu.com",content,
"text/html","utf-8","file:///android_asset/aboutus.html");
常用的函数:
(1)wv.setWebChromeClient()方法
/*
WebChromeClient 主要处理解析 渲染网页等浏览器操作的事情
WebChromeClient 帮助webview处理JavaScript的对话框 网站的图标 标题 加载进度等
*/
wv.setWebChromeClient(new WebChromeClient());
wv.setWebChromeClient(new WebChromeClient(){
//默认webview中alert弹不出来的 onJsAlert处理alert对话框
@Override
public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.mipmap.ic_launcher);
builder.setMessage(message);
builder.setTitle("提示:");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
result.confirm();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
result.cancel();
}
});
builder.create().show();
return true;
}
//设置应用程序标题
@Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
}

@Override
public void onReceivedIcon(WebView view, Bitmap icon) {
super.onReceivedIcon(view, icon);
}
//表示进度加载时 标题显示为Loading.....,加载完成后标题为Loading ok
@Override
public void onProgressChanged(WebView view, int newProgress) {
MainActivity.this.setTitle("Loading....");
if(newProgress==100){
MainActivity.this.setTitle("Loading ok");
}
}
});
(2)wv.setWebViewClient()方法
//点击网页中的超链接时不弹出浏览器的窗口 在webview控件中加载url
wv.setWebViewClient(new WebViewClient());
/*
WebViewClient 帮助webview处理各种通知、请求事件等操作
*/
wv.setWebViewClient(new WebViewClient(){
//点击webview中的链接 报告错误信息
@Override
public void onReceivedError(WebView view, WebResourceRequest request,
WebResourceError error) {
Toast.makeText(MainActivity.this,error.getDescription().toString(),
Toast.LENGTH_LONG).show();
}
//表示事件开始载入页面时调用 例如:可以设置一个loading...的界面 告诉用户程序正在加载
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
//表示页面加载完成时回调的函数 加载完成后隐藏 progressBar
@Override
public void onPageFinished(WebView view, String url) {
progressBar.setVisibility(View.GONE);
}
//当加载的网页需要重定向回调该函数 true表示由主程序控制界面加载
//false表示webview自己接受webClient的实例进行控制
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
wv.loadUrl(url);
return true;
}
});
0 0
原创粉丝点击