您的位置:首页 > 移动开发 > Android开发

android开发步步为营之78:自定义样式ProgressBar

2015-09-22 21:00 597 查看
android开发过程中,系统控件那些自带的样式,一般是不满足项目需求,一般都需要自定义样式,ProgressBar作为显示进度控件,一般也是需要设计自己的样式的。本文给出一个自定义样式。

效果如下:



第一步、自定义样式,在drawable文件夹下新建progress_style.xml

可以看到有background,就是那条白色的背景,progress就是那条蓝色的了,secondaryprogress就是那条黄色的,一般我们使用progress就可以了。gradient翻译过来就是渐变,angle是渐变的角度,startColor起始颜色,centerColor中间颜色,endColor结尾颜色,centerY根据翻译是渐变中心相对Y坐标。另外,如果只改变progress那条进度条,也可以这么简单的设置

<?xml version="1.0" encoding="utf-8"?>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<item android:id="@android:id/progress" android:drawable="@drawable/progressbar" />

</layer-list>

progress_style.xml代码如下:

<span style="font-size:14px;"><?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="270"
android:centerColor="#4cffffff"
android:centerY="0.75"
android:endColor="#4cffffff"
android:startColor="#4cffffff" />
</shape>
</item>
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:angle="270"
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="5dip" />
<gradient
android:angle="270"
android:centerColor="#ff537ff7"
android:centerY="0.75"
android:endColor="#ff537ff7"
android:startColor="#ffaec4fe" />
</shape>
</clip>

</item>

</layer-list></span>


第二步、ProgressBar引用新样式

<span style="font-size:14px;"><ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="20dp"
android:id="@+id/progressBar"
android:layout_below="@+id/textView1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="123dp"
android:progress="30"
android:secondaryProgress="60"
android:background="#ffffff"
android:progressDrawable="@drawable/progress_style"
/></span>
关于gradient的说明:

<gradient>

Specifies a gradient color for the shape.

attributes:

android:angle

Integer. The angle for the gradient, in degrees. 0 is left to right, 90 is bottom to top. It must be a multiple of 45. Default is 0.

android:centerX

Float. The relative X-position for the center of the gradient (0 - 1.0).

android:centerY

Float. The relative Y-position for the center of the gradient (0 - 1.0).

android:centerColor

Color. Optional color that comes between the start and end colors, as a hexadecimal value or color resource.

android:endColor

Color. The ending color, as a hexadecimal value or color resource.

android:gradientRadius

Float. The radius for the gradient. Only applied when android:type="radial".

android:startColor

Color. The starting color, as a hexadecimal value or color resource.

另外关于shape的官方参考文档详见:

shape参考文档
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: