最近在android项目中使用webview嵌套了一个抽奖活动网页,活动上线,运行良好(改了N次需求和突发bug),还好这种模式的活动,只需要修改网页,不需要重新打包发布市场,这也是这种模式开发的优势之一。后来据产品哥反馈说加载网页无进度提示,好吧,这个当时真没考虑这么多,这个要加加..想当然以为轻松搞定之....其实还是比轻松要复杂点...
1、首先自定义一个WebView控件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
/** * 带进度条的Webivew * @author lirunzi@.com */ @SuppressWarnings ( "deprecation" ) public class ProgressWebView extends WebView { private final static String TAG = ProgressWebView. class .getSimpleName(); private ProgressBar progressBar; private Context context; public ProgressWebView(Context context, AttributeSet attrs) { super (context, attrs); this .context = context; progressBar = new ProgressBar(context, null , android.R.attr.progressBarStyleHorizontal); progressBar.setLayoutParams( new AbsoluteLayout.LayoutParams(AbsoluteLayout.LayoutParams.MATCH_PARENT, , , )); progressBar.setProgressDrawable(getResources().getDrawable(R.drawable.wevbview_progressbar)); addView(progressBar); setWebChromeClient( new WebChromeClient()); } public class WebChromeClient extends android.webkit.WebChromeClient { @Override public void onProgressChanged(WebView view, int newProgress) { Log.d(TAG, "newProgress" + newProgress); if (newProgress == ) { progressBar.setVisibility(GONE); } else { if (progressBar.getVisibility() == GONE) progressBar.setVisibility(VISIBLE); progressBar.setProgress(newProgress); } super .onProgressChanged(view, newProgress); } // 处理javascript中的console.log @Override public boolean onConsoleMessage(ConsoleMessage cm){ android.util.Log.d(TAG, "webview console " + cm.lineNumber() + " of " + cm.sourceId() + " : " + cm.message()); return true ; } // 处理javascript中的alert() @Override public boolean onJsAlert(WebView view, String url, String message, JsResult result) { ToastUtil.showMessage(context, message, Toast.LENGTH_SHORT, Gravity.CENTER); result.cancel(); return true ; } } @Override protected void onScrollChanged( int l, int t, int oldl, int oldt) { LayoutParams lp = (LayoutParams) progressBar.getLayoutParams(); lp.x = l; lp.y = t; progressBar.setLayoutParams(lp); super .onScrollChanged(l, t, oldl, oldt); } } |
2、在需要使用webview的layout文件中引入这个控件
1
2
3
4
|
<cn.net.huami.ui.view.ProgressWebView android:id= "@+id/them_webview" android:layout_width= "match_parent" android:layout_height= "match_parent" /> |
3、添加个drawable文件,修改progress控制的进度条样式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<?xml version= "." encoding= "utf-" ?> <layer-list xmlns:android= "http://schemas.android.com/apk/res/android" > <!-- 背景 --> <item android:id= "@android:id/background" > <shape> <solid android:color= "@color/default_bg" /> </shape> </item> <!-- 进度条 --> <item android:id= "@android:id/progress" > <clip> <shape> <solid android:color= "#EAE" /> </shape> </clip> </item> </layer-list> |
4、在activity或fragment中使用这个控件的相关代码
1
2
3
4
5
6
7
8
9
|
ProgressWebView webView = (ProgressWebView)findViewById(R.id.them_webview); webView.setDownloadListener( new DownloadListener() { @Override public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) { if (url != null && url.startsWith( "http://" )) startActivity( new Intent(Intent.ACTION_VIEW, Uri.parse(url))); } }); webView.loadUrl( "网页url" ); |
通过以上代码实现了 Webview添加网页加载进度条的相关功能,希望对大家有所帮助。