如图,在进行webview加载网页的同时,在上方加载了一个动态的进度条,当页面加载完毕后,进度条消失。。。
方法:
1,重写webView
<span style="font-size:24px;">public class ProgressWebView extends WebView{
private ProgressBar progressbar;
public ProgressWebView(Context context, AttributeSet attrs) {
super(context, attrs);
progressbar = new ProgressBar(context, null, android.R.attr.progressBarStyleHorizontal);
progressbar.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 10, 0, 0));
progressbar.setProgressDrawable(context.getResources().getDrawable(R.drawable.progress_bar));
addView(progressbar);
// setWebViewClient(new WebViewClient(){});
setWebChromeClient(new WebChromeClient());
setWebViewClient(new MyWebViewClient());
}
//浏览器
private class WebChromeClient extends android.webkit.WebChromeClient {
@Override
public void onProgressChanged(WebView view, int newProgress) {
if (newProgress == 100) {
progressbar.setVisibility(GONE);
} else {
if (progressbar.getVisibility() == GONE)
progressbar.setVisibility(VISIBLE);
progressbar.setProgress(newProgress);
}
super.onProgressChanged(view, newProgress);
}
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
Log.i("TAG","onReceivedError");
}
}
@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);
}
}</span>
2,存在于Drawable中的Xml文件
<span style="font-size:24px;"><?xml version="1.0" encoding="utf-8" ?>
<layer-list xmlns:android="http:///apk/res/android" >
<!-- <item android:id="@android:id/background">
<shape>
<corners android:radius="5dip" />
<gradient
android:angle="0"
android:centerColor="#ff5a5d5a"
android:centerY="0.75"
android:endColor="#ff747674"
android:startColor="#ff9d9e9d" />
</shape>
</item>-->
<!--缓冲的进度条-->
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:angle="0"
android:centerColor="#80ffb600"
android:centerY="0.75"
android:endColor="#a0ffcb00"
android:startColor="#80ffd300" />
</shape>
</clip>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:angle="0"
android:endColor="#8000ff00"
android:startColor="#80ff0000" />
</shape>
</clip>
</item>
</layer-list></span>
3,引用webview
<ggcartoon.yztc.com.View.ProgressWebView
android:id="@+id/web"
android:layout_margin="5dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></ggcartoon.yztc.com.View.ProgressWebView>
4,代码显示
//WebView加载web资源
webview.loadUrl("http://www.bilibili.com/");
//启用支持javascript
webview.getSettings().setJavaScriptEnabled(true);