最近项目中用到WebView来显示网页, 一般浏览器加载网页都会显示进度,所以想在使用WebView时也能显示加载进度,现在尝试几种实现方法 ~~
一。利用Window的进度条
该种方法显示的进度条属于Activity的,即在Activity的标题栏与状态栏之间那进度条,显然,Activity必须要有TitleBar。
然后,为Activity设置进度条属性如下Feature:
getWindow().requestFeature(Window.FEATURE_PROGRESS);
最后,调用setProgress(int progress)来设置进度条的进度。
具体的代码如下:
package com.Hunman.mywebview; import android.app.Activity; import android.os.Bundle; import android.os.Message; import android.view.Window; import android.webkit.WebChromeClient; import android.webkit.WebView; import android.widget.LinearLayout; import android.widget.LinearLayout.LayoutParams; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.activity_main); this.getWindow().requestFeature(Window.FEATURE_PROGRESS); LinearLayout layout = new LinearLayout(this); LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); WebView webView = new WebView(this); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setSupportZoom(true); webView.requestFocus(); webView.setWebChromeClient(mWebChromeClient); webView.loadUrl("http://site.baidu.com/"); layout.addView(webView, params); setContentView(layout); } private WebChromeClient mWebChromeClient = new WebChromeClient() { @Override public void onProgressChanged(WebView view, int newProgress) { setProgress(newProgress*100); super.onProgressChanged(view, newProgress); } @Override public void onReceivedTitle(WebView view, String title) { // TODO Auto-generated method stub super.onReceivedTitle(view, title); } @Override public void onReceivedTouchIconUrl(WebView view, String url, boolean precomposed) { // TODO Auto-generated method stub super.onReceivedTouchIconUrl(view, url, precomposed); } @Override public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) { // TODO Auto-generated method stub return super.onCreateWindow(view, isDialog, isUserGesture, resultMsg); } }; }
二。 重写WebView中添加进度条
首先,重写webView控件,重写控件的方法不多说,网上一堆堆;
然后,在WebView中添加Progress控件,也很简单:
mProgressbar = new ProgressBar(context, null, android.R.attr.progressBarStyleHorizontal);
mProgressbar.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 10, 0, 0));
addView(mProgressbar);
再然后,在WebChromeClient中设置进度的改变;
最后,还要在onScrollChanged()中设置Progress位置,否则WebView向下滚动网页,进度条也会滚动隐藏,不适合用户体验。
下面直接看WebView重写后的代码:
package com.Hunman.mywebview; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Context; import android.os.Message; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; import android.widget.ProgressBar; public class AdsWebView extends WebView { private static final String TAG = "wxx"; private String mUrl; ProgressBar mProgressbar; public AdsWebView(Context context, String url) { super(context); this.mUrl = url; init(context); } @SuppressLint({ "SetJavaScriptEnabled", "NewApi" }) private void init(Context context) { //添加进度 条 mProgressbar = new ProgressBar(context, null, android.R.attr.progressBarStyleHorizontal); mProgressbar.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 10, 0, 0)); addView(mProgressbar); WebSettings webSettings = this.getSettings(); webSettings.setJavaScriptEnabled(true); webSettings.setSupportZoom(true); //webSettings.setUseWideViewPort(true); this.requestFocus(); this.setWebChromeClient(mWebChromeClient); this.loadUrl(mUrl); this.onResume(); } private WebChromeClient mWebChromeClient = new WebChromeClient() { @Override public void onProgressChanged(WebView view, int newProgress) { if (newProgress == 100) { mProgressbar.setVisibility(GONE); } else { if (mProgressbar.getVisibility() == GONE) mProgressbar.setVisibility(VISIBLE); mProgressbar.setProgress(newProgress); } super.onProgressChanged(view, newProgress); } @Override public void onReceivedTitle(WebView view, String title) { // TODO Auto-generated method stub super.onReceivedTitle(view, title); } @Override public void onReceivedTouchIconUrl(WebView view, String url, boolean precomposed) { // TODO Auto-generated method stub super.onReceivedTouchIconUrl(view, url, precomposed); } @Override public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) { // TODO Auto-generated method stub return super.onCreateWindow(view, isDialog, isUserGesture, resultMsg); } }; //保证向下流动webView时,Progress正常显示,而非隐藏 @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { // TODO Auto-generated method stub LayoutParams lp = (LayoutParams) mProgressbar.getLayoutParams(); lp.x = l; lp.y = t; mProgressbar.setLayoutParams(lp); super.onScrollChanged(l, t, oldl, oldt); } }
三。其他方法
比如,把progress和webview在xml文件中布局,再重写Webview;
又如,弹出Dialog显示进度条,等 等 方法。这些方法自己尝试实现吧。。
下篇博客,会对WebView的使用实现进行全面讲解。欢迎参考。