在实际的工作中经常需要根据美工的设计来改变一些控件的颜色,式样,以及背景图片,下面和大家共同探讨一下在Android中如何自定义SeekBar的背景颜色,进度条的颜色,以及滑块的图片

首先在layout文件夹中的main.xml内容如下

   1. <?xml version="1.0" encoding="utf-8"?>   2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   3.         android:orientation="vertical" android:layout_width="fill_parent"   4.         android:layout_height="fill_parent">   5.   6.         <SeekBar android:id="@+id/seek" android:layout_width="300px"   7.                 android:layout_height="wrap_content" android:max="100"   8.                 android:progress="50" android:progressDrawable="@drawable/seekbar_img"   9.                 android:thumb="@drawable/thumb" />  10. </LinearLayout>

注意它的 android:progressDrawable="@drawable/seekbar_img" 以及 android:thumb="@drawable/thumb" 它们分别对应的是 进度条的图片以及拖动滑块的图片,这里的图片也可以是我们自定义的drawable中的xml文件,可以理解成这两个属性应该如何显示的意思,而 @drawable/seekbar_img和@drawable/thumb它们分别对应着 drawable 文件夹中的文件seekbar_img.xml和thumb.xml,它们表示着如何去显示进度条与滑块

系统中自带的seek_thumb.xml,内容如下

   1. <?xml version="1.0" encoding="utf-8"?>   2. <!-- Copyright (C) 2008 The Android Open Source Project   3.   4.      Licensed under the Apache License, Version 2.0 (the "License");   5.      you may not use this file except in compliance with the License.   6.      You may obtain a copy of the License at   7.   8.           http://www.apache.org/licenses/LICENSE-2.0   9.  10.      Unless required by applicable law or agreed to in writing, software  11.      distributed under the License is distributed on an "AS IS" BASIS,  12.      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  13.      See the License for the specific language governing permissions and  14.      limitations under the License.  15. -->  16.  17. <!-- This is the thumb on the seek bar. -->  18. <selector xmlns:android="http://schemas.android.com/apk/res/android">  19.  20.     <item android:state_pressed="true"  21.           android:state_window_focused="true"  22.           android:drawable="@drawable/seek_thumb_pressed" />  23.  24.     <item android:state_focused="true"  25.           android:state_window_focused="true"  26.           android:drawable="@drawable/seek_thumb_selected" />  27.  28.     <item android:state_selected="true"  29.           android:state_window_focused="true"  30.           android:drawable="@drawable/seek_thumb_selected" />  31.  32.     <item android:drawable="@drawable/seek_thumb_normal" />  33.  34. </selector>

系统中自带的progress_horizontal.xml,内容如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!-- Copyright (C) 2008 The Android Open Source Project

  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at

  6. http://www.apache.org/licenses/LICENSE-2.0

  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. -->

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

  14. <item android:id="@android:id/background">
  15. <shape>
  16. <corners android:radius="5dip" />
  17. <gradient
  18. android:startColor="#ff9d9e9d"
  19. android:centerColor="#ff5a5d5a"
  20. android:centerY="0.75"
  21. android:endColor="#ff747674"
  22. android:angle="270"
  23. />
  24. </shape>
  25. </item>

  26. <item android:id="@android:id/secondaryProgress">
  27. <clip>
  28. <shape>
  29. <corners android:radius="5dip" />
  30. <gradient
  31. android:startColor="#80ffd300"
  32. android:centerColor="#80ffb600"
  33. android:centerY="0.75"
  34. android:endColor="#a0ffcb00"
  35. android:angle="270"
  36. />
  37. </shape>
  38. </clip>
  39. </item>

  40. <item android:id="@android:id/progress">
  41. <clip>
  42. <shape>
  43. <corners android:radius="5dip" />
  44. <gradient
  45. android:startColor="#ffffd300"
  46. android:centerColor="#ffffb600"
  47. android:centerY="0.75"
  48. android:endColor="#ffffcb00"
  49. android:angle="270"
  50. />
  51. </shape>
  52. </clip>
  53. </item>

  54. </layer-list>
  55. 有了这两个文件的源代码,我们就可以依样画葫芦了
    首先在自己的工程下drawable文件夹中建立seek_bar.xml文件与thumb.xml文件
    我写的两个文件内容如下seekbar_img.xml
  56.    1. <?xml version="1.0" encoding="utf-8"?>   2. <layer-list xmlns:android="http://schemas.android.com/apk/res/android">   3.         <!-- 背景图 -->   4.         <item android:id="@+android:id/background" android:drawable="@drawable/bg" />   5.         <!--全部能量图  -->   6.         <item android:id="@+android:id/SecondaryProgress"   7.                 android:drawable="@drawable/bg" />   8.         <!-- 进和能量图 -->   9.         <item android:id="@+android:id/progress" android:drawable="@drawable/bg2" />  10. </layer-list>
    thumb.xml
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
    3. <!-- 按下状态 -->
    4. <item android:state_pressed="true" android:drawable="@drawable/bg3" />

    5. <!-- 普通无焦点状态 -->
    6. <item android:state_focused="false" android:state_pressed="false"
    7. android:drawable="@drawable/bg4" />
    8. </selector>




更多相关文章

  1. Android布局文件属性笔记
  2. adb设备连接以及文件拷贝
  3. Android 开机图片/文字/动画 修改
  4. Android中String资源文件的format方法
  5. Qt for Android 调用android原生接口分享图片或文字
  6. android XMl 解析神奇xstream 四: 将复杂的xml文件解析为对象
  7. Android获取软键盘输入内容

随机推荐

  1. android上传文件到服务器
  2. Android官方文档翻译 三 1.1Creating an
  3. Android学习笔记(7)————Android中的
  4. Android多指触摸
  5. android avoiding-memory-leaks
  6. android 柱状图(带动画的)
  7. android电量状态获取
  8. android > SMS 短信数据库访问
  9. Android各类路径获取方式
  10. ViewPager fragment android tab选项卡的