标准的XML界面
代码如下 | 复制代码 |
android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> 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"> android:id="@+id/webview" android:layout_width="match_parent" android:layout_height="match_parent"/> |
上面声明了两个控件,一个是progressBar 一个是 webview,progressbar用来显示webview控件的加载进度的
值得注意的是我们重写的progressdrawable这个属性,把原来难看的加载条,稍稍美化了一些,下面就是xml代码:
代码如下 | 复制代码 |
android:startColor="#ff0000" android:centerColor="#ffa600" android:endColor="#ff5500" /> android:startColor="#234" android:centerColor="#234" android:endColor="#a24" /> android:startColor="#33000001" android:centerColor="#40000000" android:endColor="#44000000" /> |
下面是Activity的java代码:
代码如下 | 复制代码 |
ProgressBar pb; @Override protectedvoidonCreate(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(newWebViewClient() ); webView.loadUrl("http://www.x.com"); } privateclassWebViewClientextendsWebChromeClient { @Override publicvoidonProgressChanged(WebView view,intnewProgress) { pb.setProgress(newProgress); if(newProgress==100){ pb.setVisibility(View.GONE); } super.onProgressChanged(view, newProgress); } } |