欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android编程实现WebView添加进度条的方法

程序员文章站 2023-12-22 18:59:58
本文实例讲述了android编程实现webview添加进度条的方法。分享给大家供大家参考,具体如下: 标准的xml界面

本文实例讲述了android编程实现webview添加进度条的方法。分享给大家供大家参考,具体如下:

标准的xml界面

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical" >
 <progressbar
  android:id="@+id/pb"
  style="?android:attr/progressbarstylehorizontal"
  android:layout_width="fill_parent"
  android:layout_height="8dip"
  android:indeterminateonly="false"
  android:max="100"
  android:progressdrawable="@drawable/progress_bar_states" >
 </progressbar>
 <webview
  android:id="@+id/webview"
  android:layout_width="match_parent"
  android:layout_height="match_parent" />
</linearlayout>

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

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

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:id="@android:id/background">
  <shape>
   <gradient
     android:startcolor="#ff0000"
     android:centercolor="#ffa600"
     android:endcolor="#ff5500"
   />
  </shape>
 </item>
 <item android:id="@android:id/secondaryprogress">
  <clip>
   <shape>
    <gradient
      android:startcolor="#234"
      android:centercolor="#234"
      android:endcolor="#a24"
    />
   </shape>
  </clip>
 </item>
 <item android:id="@android:id/progress">
  <clip>
   <shape>
    <gradient
     android:startcolor="#33000001"
     android:centercolor="#40000000"
     android:endcolor="#44000000"
    />
   </shape>
  </clip>
 </item>
</layer-list>

下面是activity的java代码:

progressbar pb;
@override
protected void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.xxx);
 pb = (progressbar) findviewbyid(r.id.pb);
 pb.setmax(100);
 webview webview = (webview) findviewbyid(r.id.webview);
 webview.getsettings().setjavascriptenabled(true);
 webview.getsettings().setsupportzoom(true);
 webview.getsettings().setbuiltinzoomcontrols(true);
 webview.setwebchromeclient(new webviewclient() );
 webview.loadurl("http://www.x.com");
}
private class webviewclient extends webchromeclient {
 @override
 public void onprogresschanged(webview view, int newprogress) {
  pb.setprogress(newprogress);
  if(newprogress==100){
   pb.setvisibility(view.gone);
  }
  super.onprogresschanged(view, newprogress);
 }
}

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

更多关于android相关内容感兴趣的读者可查看本站专题:《android视图view技巧总结》、《android开发动画技巧汇总》、《android编程之activity操作技巧总结》、《android布局layout技巧总结》、《android开发入门与进阶教程》、《android资源操作技巧汇总》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。

上一篇:

下一篇: