2011-06-04 61 views
7

我想在android中自定義seekbar。在android中自定義seekbar

enter image description here

任何人都可以指導我如何能夠做到?我是android新手。

+1

的回答你的問題貌似http://stackoverflow.com/questions/3480456/seek-bar-change-path-color-from-yel​​low-to的副本-white – Asahi 2011-06-04 12:17:33

+0

您正在尋找什麼樣的定製,您能澄清嗎? – midhunhk 2012-08-29 13:40:02

回答

2
// ------------ custom seekbar 
LayerDrawable layer = (LayerDrawable) verticalSeekBar_1 
.getProgressDrawable(); 

Drawable draw1 = (Drawable) layer 
.findDrawableByLayerId(android.R.id.progress); 

Bitmap bitmap1 = BitmapFactory.decodeResource(getApplicationContext() 
.getResources(), R.drawable.scroll_on); 

draw1 = new ClipDrawable(new BitmapDrawable(bitmap1), 
Gravity.AXIS_PULL_BEFORE, ClipDrawable.HORIZONTAL); 

layer.setDrawableByLayerId(android.R.id.progress, draw1); 

Drawable draw2 = (Drawable) layer 
.findDrawableByLayerId(android.R.id.background); 

Bitmap bitmap2 = BitmapFactory.decodeResource(getApplicationContext() 
.getResources(), R.drawable.scroll_off); 

draw2 = new BitmapDrawable(bitmap2); 
layer.setDrawableByLayerId(android.R.id.background, draw2); 

// -------------- 
+0

優秀。由於圖像可能來自網絡。我必須知道如何以編程方式自定義seekbar,但不在xml中。 – Yeung 2014-01-23 08:06:39

18
<SeekBar 
       android:id="@+id/seekBar1" 
       android:layout_width="match_parent" 
       android:layout_height="34dip" 
       android:progressDrawable="@drawable/strip" 
       android:thumb="@drawable/icon_small" 
       android:layout_weight="3.0" /> 

您可以簡單地設置progressDrawble,拇指和backgroung屬性,你會得到你想要的。

+3

這是一個關於如何完成的例子: http://www.mokasocial.com/2011/02/create-a-custom-styled-ui-slider-seekbar-in-android/ – 2012-06-06 06:12:44

1

XML代碼

<SeekBar 
    android:id="@+id/seekBar1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:progressDrawable="@drawable/seek_bar" 
    android:paddingLeft="15dp" 
    android:paddingRight="15dp" 
    android:thumb="@drawable/thumbler_small" 
    android:indeterminate="false" /> 

seek_bar.xml

<?xml version="1.0" encoding="UTF-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item 
     android:id="@+android:id/SecondaryProgress" 
     android:drawable="@drawable/first"/> 
    <item 
     android:id="@+android:id/progress" 
     android:drawable="@drawable/second"/> 
</layer-list> 

第一可拉伸是空的或未示出顏色充分的區域。

第二個可繪製的是填充或顏色全部區域。

this鏈接幫助您更多。