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代码如下:
第二步、ProgressBar引用新样式
<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参考文档
效果如下:
第一步、自定义样式,在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参考文档
相关文章推荐
- Android推送服务(使用第三方api需要注意的地方)
- (4.4.1.8) android垃圾回收机制及程序优化System.gc
- Android应用环境搭建
- 0922Android传感器实例指南针
- Android游戏开发研究帧动画实现
- Android 位置服务——用户定位User Location
- android拍摄视频的实现
- Android--Service之提高
- Android第三方控件——百度地图的使用
- 在Android中使用IOC框架让代码更清爽
- SoundPool的使用技巧
- Android组件系列----Intent详解
- Android基础入门教程——2.4.7 构建一个可复用的自定义BaseAdapter
- Android传感器的使用开发、简易指南针
- Android工程目录结构
- Android读写操作之sdcard的读写操作
- Android开发之闹钟
- Android读写操作之内存的读写操作
- Android新建项目 默认布局改为 LinearLayout
- Android 对程序异常崩溃的捕捉