最近用到webView加载网页的功能,但是在空白页面等的太久了于是就添加一个进度条缓解一下等待的焦虑
添加之后发现系统的ProgressBar的样式有点不尽如人意,所以就自定义了一个样式,先来看看效果图:
代码:
1,首先在styles.xml中增加自定义的样式MyProgressBar
<style name="MyProgressBar" parent="@android:style/Widget.ProgressBar.Horizontal">
<item name="android:maxHeight">5dip</item>
<item name="android:minHeight">2dip</item>
<item name="android:indeterminateOnly">false</item>
<item name="android:indeterminateDrawable">@android:drawable/progress_indeterminate_horizontal</item>
<item name="android:progressDrawable">@drawable/progressbar_bg</item>
</style>
2,在drawable中新建progressbar_bg.xml资源
<?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="5dip" />
<gradient
android:angle="0"
android:endColor="#00000000"
android:startColor="#00000000" />
</shape>
</item>
<item android:id="@android:id/progress">
<clip>
<scale
android:scaleWidth="100%"
android:drawable="@drawable/my_title_bg"/>
</clip>
</item>
</layer-list>
3,在这里的背景图我用了一个渐变资源也是在drawable中my_title_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!--left to right-->
<gradient
android:angle="0"
android:startColor="@color/color_FE8E51"
android:endColor="@color/color_FF4358"/>
</shape>
到这里基本资源就搞定了,接下来在布局文件中使用
<ProgressBar
android:id="@+id/lh_pb"
style="@style/MyProgressBar"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_2" />
运行之后就可以看到效果啦