ProgressBar 默认样式很多时候是不满足项目需求的,简单粗暴点说,就是有些丑。
下面展示一下自定义ProgressBar的过程:
首先,在styles.xml文件中添加你的自定义ProgressBar样式。@drawable/progressbar_h是一个自定义的进度条背景。
<style name="MyProgressBar" parent="@android:style/Widget.ProgressBar.Horizontal">
<item name="android:maxHeight">50dip</item>
<item name="android:minHeight">10dip</item>
<item name="android:indeterminateOnly">false</item>
<item name="android:indeterminateDrawable">@android:drawable/progress_indeterminate_horizontal</item>
<item name="android:progressDrawable">@drawable/progressbar_h1</item>
</style>
progressbar_h1.xml :(PS :注意放在drawable目录下)这个文件是一个渐变的背景
<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/progressbar_h_shape1"/>
</clip>
</item>
</layer-list>
background:设置进度条背景为透明色,
progress:设置进度条填充色。其中scaleWidth可以让背景进行缩放,而不是右侧截断的效果。
progressbar_h_shape1.xml: 渐变的颜色
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="5dip" />
<gradient
android:angle="0"
android:endColor="#ecd803"
android:startColor="#f2eba1" />
</shape>
只需要在布局中指定ProgressBar的style属性即可,如下所示:
<ProgressBar
android:layout_width="match_parent"
android:layout_height="8dp"
android:progress="34"
style="@style/MyProgressBar"/>
运行效果: