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

 

 

 

 

 

 

 

 

發佈了48 篇原創文章 · 獲贊 8 · 訪問量 45萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章