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 {
@Override
public 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);
}
}
写完,收工!