WebView添加进度条

标准的XML界面

  1. <?xml version=“1.0” encoding=“utf-8″?>
  2. <LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
  3.     android:layout_width=“match_parent”
  4.     android:layout_height=“match_parent”
  5.     android:orientation=“vertical” >
  6.     <ProgressBar
  7.         android:id=“@+id/pb”
  8.         style=“?android:attr/progressBarStyleHorizontal”
  9.         android:layout_width=“fill_parent”
  10.         android:layout_height=“8dip”
  11.         android:indeterminateOnly=“false”
  12.         android:max=“100”
  13.         android:progressDrawable=“@drawable/progress_bar_states” >
  14.     </ProgressBar>
  15.     <WebView
  16.         android:id=“@+id/webview”
  17.         android:layout_width=“match_parent”
  18.         android:layout_height=“match_parent” />
  19. </LinearLayout>

上面声明了两个控件,一个是progressBar 一个是 webview,progressbar用来显示webview控件的加载进度的

值得注意的是我们重写的progressdrawable这个属性,把原来难看的加载条,稍稍美化了一些,下面就是xml代码:

  1. <layer-list xmlns:android=“http://schemas.android.com/apk/res/android”>
  2.     <item android:id=“@android:id/background”>
  3.         <shape>
  4.             <gradient
  5.                     android:startColor=“#ff0000″
  6.                     android:centerColor=“#ffa600″
  7.                     android:endColor=“#ff5500″
  8.             />
  9.         </shape>
  10.     </item>
  11.     <item android:id=“@android:id/secondaryProgress”>
  12.         <clip>
  13.             <shape>
  14.                 <gradient
  15.                         android:startColor=“#234″
  16.                         android:centerColor=“#234″
  17.                         android:endColor=“#a24″
  18.                 />
  19.             </shape>
  20.         </clip>
  21.     </item>
  22.     <item android:id=“@android:id/progress”>
  23.         <clip>
  24.             <shape>
  25.                 <gradient
  26.                     android:startColor=“#33000001″
  27.                     android:centerColor=“#40000000″
  28.                     android:endColor=“#44000000″
  29.                 />
  30.             </shape>
  31.         </clip>
  32.     </item>
  33. </layer-list>

下面是Activity的java代码:

  1. ProgressBar pb;
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4.     super.onCreate(savedInstanceState);
  5.     setContentView(R.layout.xxx);
  6.     pb = (ProgressBar) findViewById(R.id.pb);
  7.     pb.setMax(100);
  8.     WebView webView = (WebView) findViewById(R.id.webview);
  9.     webView.getSettings().setJavaScriptEnabled(true);
  10.     webView.getSettings().setSupportZoom(true);
  11.     webView.getSettings().setBuiltInZoomControls(true);
  12.     webView.setWebChromeClient(new WebViewClient() );
  13.     webView.loadUrl(“http://www.x.com”);
  14. }
  15. private class WebViewClient extends WebChromeClient {
  16.     @Override
  17.     public void onProgressChanged(WebView view, int newProgress) {
  18.         pb.setProgress(newProgress);
  19.         if(newProgress==100){
  20.             pb.setVisibility(View.GONE);
  21.         }
  22.         super.onProgressChanged(view, newProgress);
  23.     }
  24. }

关键地方是重写了一个webchromeclient中的onprogressChange方法,这样我们就能控制progress的进度啦,是不是很方便的,京东也是这么干的哦,快去试一试吧

 

转自:http://blog.csdn.net/zzcchunter/article/details/9182893

 

猜您喜欢

要发表评论,您必须先登录