WebView组件的应用
1.什么是WebView?
WebView(网络视图)能加载显示网页,可以将其视为一个浏览器,它使用了WebKit渲染引擎加载显示网页。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="@dimen/activity_horizontal_margin" android:layout_marginRight="@dimen/activity_horizontal_margin" android:layout_marginTop="@dimen/activity_vertical_margin" android:layout_marginBottom="@dimen/activity_vertical_margin" > <WebView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/wv1" /> </LinearLayout>
使用的尺寸资源:
<?xml version="1.0" encoding="utf-8"?> <resources> <dimen name="activity_vertical_margin">10dp</dimen> <dimen name="activity_horizontal_margin">10dp</dimen> </resources>
实例化webview
wv1=(WebView)findViewById(R.id.wv1);
wv1.loadUrl(url);//载入url,(系统自带浏览器,要用webview需要重写方法)
websettings的设置//还可以设置很多东西
WebSettings websettings=wv1.getSettings();//获取配置信息
websettings.setJavaScriptEnabled(true);//设置脚本可以使用
websettings.setCacheMode(websettings.LOAD_CACHE_ELSE_NETWORK);//打开缓存
wv1.setWebViewClient( new WebViewClient() { //需要重写shouldOverrideUrlLoading public boolean shouldOverrideUrlLoading(WebView view,String url) { view.loadUrl(url); return true; } });
这样基本完成了,但卡顿现象明显,用户体验感太差,所以我们添加一些方法。
package com.example.webview; import android.app.Activity; import android.app.ProgressDialog; import android.os.Bundle; import android.view.KeyEvent; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; public class MainActivity extends Activity { private WebView wv1=null; String url="http:www.baidu.com"; private ProgressDialog progressDialog=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); wv1=(WebView)findViewById(R.id.wv1); wv1.loadUrl(url);//载入url,(系统自带浏览器,要用webview需要重写方法) WebSettings websettings=wv1.getSettings();//获取配置信息 websettings.setJavaScriptEnabled(true);//设置脚本可以使用 websettings.setCacheMode(websettings.LOAD_CACHE_ELSE_NETWORK);//打开缓存 wv1.setWebViewClient( new WebViewClient() { //需要重写shouldOverrideUrlLoading public boolean shouldOverrideUrlLoading(WebView view,String url) { view.loadUrl(url); return true; } }); wv1.setWebChromeClient( new WebChromeClient() { public void onProgressChanged(WebView view,int newProgress)//重写的方法来显示Dialog的进度条 { if (newProgress==100) { closeProgressDialog(); } else { showProgressDialog(newProgress); } super.onProgressChanged(view, newProgress); } public void closeProgressDialog()//关闭progressDialog { progressDialog.dismiss(); progressDialog=null; } public void showProgressDialog(int newProgress)//显示progressDialog { if (progressDialog==null) { progressDialog=new ProgressDialog(MainActivity.this); progressDialog.setTitle("页面加载中...请稍后...");//设置标题 progressDialog.setProgressStyle(progressDialog.STYLE_HORIZONTAL);//设置样式 progressDialog.setProgress(newProgress); progressDialog.show(); } else { progressDialog.setProgress(newProgress); progressDialog.show(); } } }); } public boolean onKeyDown(int keyCode,KeyEvent event) { if (keyCode==event.KEYCODE_BACK) { if (wv1.canGoBack()) { wv1.goBack(); return true; } else { System.exit(0); } } return super.onKeyDown(keyCode, event); } }