在实际的工作中经常需要根据美工的设计来改变一些控件的颜色,式样,以及背景图片,下面和大家共同探讨一下在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. Android控件编辑时键盘弹起与关闭处理
  3. Android之NDK开发
  4. 修改Android(安卓)Studio下的AVD的下载路径
  5. Android中对Log日志文件的分析
  6. 布局中文件中【控件间距参数详解以及单位选择】
  7. adb设备连接以及文件拷贝
  8. Android(安卓)中创建avd和sdcard
  9. Android中String资源文件的format方法

随机推荐

  1. 英特尔进击新能源发电,看AI如何用气象预报
  2. 在推荐系统中,我还有隐私吗?联邦学习:你可以
  3. CCKS 2020「基于标题的大规模商品实体检
  4. 10万字节跳动员工都在用的办公协同软件,如
  5. 让AI像Excel一样普及:这周末,我们聊了聊AI
  6. 如何从 0 到 1 构建埋点体系
  7. 微信小程序服务类目在哪里可以修改?
  8. 机器人是怎么知道如何抓握杯子的?
  9. MongoDB副本集配置hidden从库
  10. 如何使用“迁移助理”将文件从旧 Mac 移