安卓ProgressBar水平进度条的颜色设置

来源:互联网 发布:汽配数据库 编辑:程序博客网 时间:2024/06/06 17:59

安卓系统提供了水平进度条ProgressBar的样式,而我们在实际开发中,几乎不可能使用默认的样式,原因就是“太丑”^_^

所以我们在更多的时候需要对其颜色进行自定义,主要使用就是自定义样式文件。


再在drawable目录下新增progressbar.xml文件,可以设置默认背景色和进度条的颜色

(值得一提的是支持渐变色)

代码:

[html] view plaincopy
  1. <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >  
  2.   
  3.     <item android:id="@android:id/background">  
  4.   
  5.         <shape>  
  6.   
  7.             <corners android:radius="5dip" />  
  8.   
  9.             <gradient  
  10.                 android:angle="0"  
  11.                 android:centerColor="#ff5a5d5a"  
  12.                 android:centerY="0.75"  
  13.                 android:endColor="#ff747674"  
  14.                 android:startColor="#ff9d9e9d" />  
  15.         </shape>  
  16.     </item>  
  17.   
  18.     <item android:id="@android:id/secondaryProgress">  
  19.   
  20.         <clip>  
  21.   
  22.             <shape>  
  23.   
  24.                 <corners android:radius="5dip" />  
  25.   
  26.                 <gradient  
  27.                     android:angle="0"  
  28.                     android:centerColor="#80ffb600"  
  29.                     android:centerY="0.75"  
  30.                     android:endColor="#a0ffcb00"  
  31.                     android:startColor="#80ffd300" />  
  32.             </shape>  
  33.         </clip>  
  34.     </item>  
  35.   
  36.     <item android:id="@android:id/progress">  
  37.   
  38.         <clip>  
  39.   
  40.             <shape>  
  41.   
  42.                 <corners android:radius="5dip" />  
  43.   
  44.                 <gradient  
  45.                     android:angle="0"  
  46.                     android:endColor="#8000ff00"  
  47.                     android:startColor="#80ff0000" />  
  48.             </shape>  
  49.         </clip>  
  50.     </item>  
  51.   
  52. </layer-list>  

布局文件定义如下:

[html] view plaincopy
  1. <ProgressBar  
  2.     android:id="@+id/progressBar"  
  3.     style="?android:attr/progressBarStyleHorizontal"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="7.5dp"  
  6.     android:max="100"  
  7.     android:progress="80"   
  8.     android:layout_marginRight="8dp"  
  9.     android:progressDrawable="@drawable/progressbar"   
  10.     android:visibility="visible"/>  
0 0
原创粉丝点击