webview添加网页加载进度条

    最近在android项目中使用webview嵌套了一个抽奖活动网页,活动上线,运行良好(改了N次需求和突发bug),还好这种模式的活动,只需要修改网页,不需要重新打包发布市场,这也是这种模式开发的优势之一。后来据产品哥反馈说加载网页无进度提示,好吧,这个当时真没考虑这么多,这个要加加..想当然以为轻松搞定之....其实还是比轻松要复杂点...

 1、首先自定义一个WebView控件

 1 /**
 2  * 带进度条的Webivew
 3  * @author lirunzi@163.com
 4  */
 5 @SuppressWarnings("deprecation")
 6 public class ProgressWebView extends WebView {
 7     private final static String TAG = ProgressWebView.class.getSimpleName();
 8 
 9     private ProgressBar progressBar;
10     private Context context;
11 
12     public ProgressWebView(Context context, AttributeSet attrs) {
13         super(context, attrs);
14         this.context = context;
15         progressBar = new ProgressBar(context, null, android.R.attr.progressBarStyleHorizontal);
16         progressBar.setLayoutParams(new AbsoluteLayout.LayoutParams(AbsoluteLayout.LayoutParams.MATCH_PARENT, 3, 0, 0));
17         progressBar.setProgressDrawable(getResources().getDrawable(R.drawable.wevbview_progressbar));
18         addView(progressBar);
19         setWebChromeClient(new WebChromeClient());
20     }
21 
22     public class WebChromeClient extends android.webkit.WebChromeClient {
23         @Override
24         public void onProgressChanged(WebView view, int newProgress) {
25             Log.d(TAG, "newProgress" + newProgress);
26             if (newProgress == 100) {
27                 progressBar.setVisibility(GONE);
28             } else {
29                 if (progressBar.getVisibility() == GONE)
30                     progressBar.setVisibility(VISIBLE);
31                 progressBar.setProgress(newProgress);
32             }
33             super.onProgressChanged(view, newProgress);
34         }
35 
36         // 处理javascript中的console.log
37         @Override
38         public boolean onConsoleMessage(ConsoleMessage cm){
39             android.util.Log.d(TAG, "webview console " + cm.lineNumber() + " of " + cm.sourceId() + " : " + cm.message());
40             return true;
41         }
42 
43         // 处理javascript中的alert()
44         @Override
45         public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
46             ToastUtil.showMessage(context, message, Toast.LENGTH_SHORT, Gravity.CENTER);
47             result.cancel();
48             return true;
49         }
50 
51     }
52 
53     @Override
54     protected void onScrollChanged(int l, int t, int oldl, int oldt) {
55         LayoutParams lp = (LayoutParams) progressBar.getLayoutParams();
56         lp.x = l;
57         lp.y = t;
58         progressBar.setLayoutParams(lp);
59         super.onScrollChanged(l, t, oldl, oldt);
60     }
61 }

2、在需要使用webview的layout文件中引入这个控件

1  <cn.net.huami.ui.view.ProgressWebView 
2             android:id="@+id/them_webview"
3              android:layout_width="match_parent"
4              android:layout_height="match_parent" />

3、添加个drawable文件,修改progress控制的进度条样式

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
 3     <!-- 背景 -->
 4     <item android:id="@android:id/background">
 5         <shape>
 6             <solid android:color="@color/default_bg" />
 7         </shape>
 8     </item>
 9     <!-- 进度条 -->
10     <item android:id="@android:id/progress">
11         <clip>
12             <shape>
13                 <solid android:color="#2E8AE7" />
14             </shape>
15         </clip>
16     </item>
17 </layer-list>

4、在activity或fragment中使用这个控件的相关代码

 1 ProgressWebView webView = (ProgressWebView)findViewById(R.id.them_webview);
 2 
 3 webView.setDownloadListener(new DownloadListener() {
 4             @Override
 5             public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
 6                 if (url != null && url.startsWith("http://"))
 7                     startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
 8             }
 9         });
10  webView.loadUrl("网页url");
原文地址:https://www.cnblogs.com/hubli/p/4835549.html