Android中自定义SeekBar的背景颜色,进度条颜色,以及滑块的图片

在Android中的控件种类已经足够我们使用,但是有时候大家需要根据美工的设计来改变一些控件的颜色,式样,以及背景图片

最近正好有这方面的需要,用了很久时间,找到了改变基本颜色以及图片的方法
下面以SeekBar为例,为大家描述一下我的做法

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

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>  
 


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

当初我想的是在网上找SeekBar的原始样式文件是如何定义,这样就可以照搬代码,修改一些我需要的图片以及颜色和大小就行了,于是就开始搜索,这里要用到的是Android的系统源码,具体下载办法网上很多,需要用到cygwin,大家可以参考 http://tech.it168.com/a2009/0529/579/000000579026.shtml 



下好源代以后,可以在 C:\cygwin\home\android\frameworks\base\core\res\res\drawable 这个路径下找到很多图片与android的原始控件样式(即xml文件)
找一下,哈哈,好东西可不少,以后要改样式全靠它们了
我们可以在这是里面找到seek_thumb.xml,内容如下

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>  

 

它定义的是seekbar的滑块样式,内容很简单,大家应该看得懂,分别对应着按下,选中,以及获得焦点时滑块的图片


另外,我们还可以找到 progress_horizontal.xml,内容如下

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. <layer-list xmlns:android="http://schemas.android.com/apk/res/android">  
  18.       
  19.     <item android:id="@android:id/background">  
  20.         <shape>  
  21.             <corners android:radius="5dip" />  
  22.             <gradient  
  23.                     android:startColor="#ff9d9e9d"  
  24.                     android:centerColor="#ff5a5d5a"  
  25.                     android:centerY="0.75"  
  26.                     android:endColor="#ff747674"  
  27.                     android:angle="270"  
  28.             />  
  29.         </shape>  
  30.     </item>  
  31.       
  32.     <item android:id="@android:id/secondaryProgress">  
  33.         <clip>  
  34.             <shape>  
  35.                 <corners android:radius="5dip" />  
  36.                 <gradient  
  37.                         android:startColor="#80ffd300"  
  38.                         android:centerColor="#80ffb600"  
  39.                         android:centerY="0.75"  
  40.                         android:endColor="#a0ffcb00"  
  41.                         android:angle="270"  
  42.                 />  
  43.             </shape>  
  44.         </clip>  
  45.     </item>  
  46.       
  47.     <item android:id="@android:id/progress">  
  48.         <clip>  
  49.             <shape>  
  50.                 <corners android:radius="5dip" />  
  51.                 <gradient  
  52.                         android:startColor="#ffffd300"  
  53.                         android:centerColor="#ffffb600"  
  54.                         android:centerY="0.75"  
  55.                         android:endColor="#ffffcb00"  
  56.                         android:angle="270"  
  57.                 />  
  58.             </shape>  
  59.         </clip>  
  60.     </item>  
  61.       
  62. </layer-list>  
 



有了这两个文件的源代码,我们就可以依样画葫芦了
首先在自己的工程下drawable文件夹中建立seek_bar.xml文件与thumb.xml文件
我写的两个文件内容如下
seekbar_img.xml

Xml代码   收藏代码
  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

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.     <!-- 普通无焦点状态 -->  
  7.     <item android:state_focused="false" android:state_pressed="false"  
  8.         android:drawable="@drawable/bg4" />  
  9. </selector>   
 

这时运行程序,我的截图如下,丑了点,但是目的达到了
转自:http://blog.csdn.net/wangjia55/article/details/8853912

 

 

 

 

 

 

 

 

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值