android SeekBar屬性

Android的SeekBar

博客分類:  Android
 

使用SeekBar的時候,經常遇到的問題有如下:

1. seekbar的拖動按鈕沒有居中。

2. seekbar的高度有問題。

3. seekbar的拖動按鈕在最左最右顯示不全。

 

首先,要解決上述問題,要先檢查layout文件在描述seekbar時,有沒有正確設置圖片的尺寸和檢查屏幕密度與圖片是否放置在相應的drawable文件夾中(drawable-hdpi, drawable-mdpi, drawable-ldpi).

 

在確保圖片資源正確的情況下,參考如下XML:

 

layout

Xml代碼  收藏代碼
  1. <SeekBar  
  2.     android:layout_width="321px"  
  3.     android:layout_height="wrap_content"  
  4.     android:layout_centerInParent="true"  
  5.     android:maxHeight="12px"  
  6.     android:minHeight="12px"  
  7.     android:paddingLeft="18px"  
  8.     android:paddingRight="18px"  
  9.     android:max="100"  
  10.     android:progressDrawable="@drawable/seekbar_style"  
  11.     android:thumb="@drawable/drag_ball"  
  12.     android:id="@+id/seekBar"/>  

 

 seekbar_style

Xml代碼  收藏代碼
  1. <layer-list  
  2.     xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <item  
  4.         android:id="@android:id/background"  
  5.         android:drawable="@drawable/drag_bar_background"/>  
  6.     <item  
  7.         android:id="@android:id/progress"  
  8.         android:drawable="@drawable/drag_bar_foreground"/>  
  9.     <item  
  10.         android:id="@android:id/secondaryProgress"  
  11.         android:drawable="@drawable/drag_bar_foreground"/>  
  12. </layer-list>   

 

 簡單解釋下seekbar中幾個重要的屬性:

android:layout_height="wrap_content"

//建議使用wrap_content,否則一定要保證設置的值不小於seekbar圖片資源中的最高值


android:maxHeight="12px"
android:minHeight="12px"

//說明進度條的最低和最大高度,解決高度問題。


android:paddingLeft="18px"
android:paddingRight="18px"

//解決拖動按鈕在最左最右顯示不全的問題,padding的值一般是thumb的一半寬度。


android:progressDrawable="@drawable/seekbar_style"

//設置了此值,就表示使用自定義的進度條樣式,在其中可以設置進度條背景圖,進度條圖,緩衝條圖。


android:thumb="@drawable/drag_ball"
//seekbar的拖動按鈕圖片

 

 

android系統自帶的自定義樣式例子:

 

seekbar_style

Xml代碼  收藏代碼
  1. <layer-list xmlns:android="http://schemas.android.com/apk/res/android">    
  2.      <item android:id="@android:id/background">    
  3.          <shape>    
  4.              <corners android:radius="5dip" />    
  5.              <gradient    
  6.                      android:startColor="#ff9d9e9d"    
  7.                      android:centerColor="#ff5a5d5a"    
  8.                      android:centerY="0.75"    
  9.                      android:endColor="#ff747674"    
  10.                      android:angle="270"/>    
  11.          </shape>    
  12.      </item>    
  13.      <item android:id="@android:id/secondaryProgress">    
  14.          <clip>    
  15.              <shape>    
  16.                  <corners android:radius="5dip" />    
  17.                  <gradient    
  18.                          android:startColor="#80ffd300"    
  19.                          android:centerColor="#80ffb600"    
  20.                          android:centerY="0.75"    
  21.                          android:endColor="#a0ffcb00"    
  22.                          android:angle="270"/>    
  23.              </shape>    
  24.          </clip>    
  25.      </item>    
  26.      <item android:id="@android:id/progress">    
  27.          <clip>    
  28.              <shape>    
  29.                  <corners android:radius="5dip" />    
  30.                  <gradient    
  31.                          android:startColor="#ff0099CC"    
  32.                          android:centerColor="#ff3399CC"    
  33.                          android:centerY="0.75"    
  34.                          android:endColor="#ff6699CC"    
  35.                          android:angle="270"/>    
  36.              </shape>    
  37.          </clip>    
  38.      </item>    
  39. </layer-list>    

 

thumb

Xml代碼  收藏代碼
  1. <selector xmlns:android="http://schemas.android.com/apk/res/android">          
  2.          
  3.     <!-- 按下狀態-->    
  4.     <item      
  5.         android:state_focused="true"      
  6.         android:state_pressed="true"      
  7.         android:drawable="@drawable/thumb_pressed" />          
  8.     <!-- 普通無焦點狀態 -->    
  9.     <item      
  10.         android:state_focused="false"      
  11.         android:state_pressed="false"    
  12.         android:drawable="@drawable/thumb_normal" />                
  13.     <!-- 有焦點狀態-->    
  14.     <item      
  15.         android:state_focused="true"      
  16.         android:state_pressed="false"                
  17.         android:drawable="@drawable/thumb_focused" />           
  18.     <!-- 有焦點 -->    
  19.     <item      
  20.         android:state_focused="true"                
  21.         android:drawable="@drawable/thumb_focused" />       
  22. </selector>    

 轉自:http://haking.iteye.com/blog/1118769

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章