Android開發模板------WebView載入時顯示ProgressBar進度條,android載入webview
ProgressBar進度條不能完全填充控制項,這讓我糾結了很久。
後來ProgressBar添加了一個屬性android:progressDrable解決了。
1、首先看下該布局:
<ProgressBar android:id="@+id/myProgressBar" style="?android:attr/progressBarStyleHorizontal" android:layout_below="@id/control_search_bar" android:layout_gravity="center_vertical" android:visibility="invisible" android:progressDrawable="@drawable/progressbar" android:layout_width="match_parent" android:layout_height="2dp"/>
2、progressbar.xml位於drawable檔案夾下:
<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@android:id/progress"> <clip> <shape> <corners android:radius="2dip" /> <gradient android:angle="0" android:endColor="#8000ff00" android:startColor="#80ff0000" /> </shape> </clip> </item></layer-list>
該檔案可以設定你自己想要的樣式,下面是參考網上樣式:
<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@android:id/background"> <shape> <corners android:radius="2dip" /> <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="2dip" /> <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="2dip" /> <gradient android:angle="0" android:endColor="#8000ff00" android:startColor="#80ff0000" /> </shape> </clip> </item></layer-list>
3、ProgressBar主要是在WebChromeClient使用:
private class MyWebChromeClient extends WebChromeClient {@Overridepublic void onProgressChanged(WebView view, int newProgress) {myProgressBar.setMax(100);if (newProgress < 100) {if (myProgressBar.getVisibility() == View.INVISIBLE) {myProgressBar.setVisibility(View.VISIBLE);}myProgressBar.setProgress(newProgress);} else {myProgressBar.setProgress(100);myProgressBar.setVisibility(View.INVISIBLE);}super.onProgressChanged(view, newProgress);}}
寫完,收工!