2012-08-16 32 views
3

如何動態地單獨垂直彩色線條添加到搜索條的進度條?加垂直的彩色線搜索欄背景繪製

我已經有progress.xml,progress_fill.xml和個人可繪製創建background_fill文件,讓我定製我的搜索條在一定程度上。但是,根據具體情況,可能需要以各種不同的顏色繪製各條垂直線條,並且可能需要在搜索欄上的任意點繪製垂直線條。它們不能在XML佈局文件中設置。

我想我需要通過編程寫一點顏色rects到background_fill.xml繪製(我progress_fill已被設置爲幾乎透明)。

我該怎麼做編程寫那些豆蔻rects我background_fill.xml繪製?

我做的一個TextView類似的東西,在我使用的SpannableStringBuilder和ImageSpan的小rects寫入到一個TextView。但我沒有看到它可用於seekbar小部件。

回答

2

通過擴展搜索欄小工具這篇。兩個步驟:首先,創建一個名爲CustomSeekBar新類來擴展搜索欄:

package com.example.seekbar; 

class CustomSeekBar extends SeekBar { 

public CustomSeekBar(Context context) { 
    super(context); 
} 

public CustomSeekBar(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

public CustomSeekBar(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
} 

protected void onDraw(Canvas canvas) { 

    super.onDraw(canvas); 

    private Paint paintRect = new Paint(); 
    paintRect.setColor(Color.rgb(142, 196, 0)); 

    private Rect audioRect = new Rect(); 
    audioRect.set(5, 7, 4, 18); 

    canvas.drawRect(audioRect, paintRect); 
} 
} 

其次,參照本CustomSeekBar在佈局XML文件:如果使用im這個技術,然後

<view class="com.example.seekbar.CustomSeekBar" 
    android:id="@+id/seekBar0" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="10dp" /> 
+0

我怎麼會從可繪製文件夾應用自定義的拇指? – Erum 2014-08-07 17:09:19