2017-07-10 53 views
1

我使用SeekBar,我需要使用不同的顏色SeekBar的列表中的進展。可繪製的xml形狀顏色變化dyanamically

所以我需要改變形狀繪製顏色編程

這是drawale XML。 seekbar_points.xml

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
<item 
    android:id="@android:id/background" 
    android:drawable="@drawable/points_bg" /> 
<item android:id="@android:id/progress"> 
    <clip android:drawable="@drawable/points_progress" /> 
</item> 
</layer-list> 

points_progress.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="line"> 
<stroke android:width="6dp" android:color="@color/colorAccent" /> 
</shape> 

points_bg.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="line"> 
<stroke android:width="6dp" android:color="@color/color7" /> 
</shape> 

enter image description here

我必須動態地改變這種綠色區域。爲了這個,我已經設置了顏色points_progress.xml在該XML

請提出這個想法提前 感謝....

回答

0

使用此:

seekBar.getProgressDrawable().setColorFilter(ContextCompat.getColor(сontext, R.color.color_new), PorterDuff.Mode.SRC_IN); 

更新:
如果你只想改變進展區的顏色:

LayerDrawable progressDrawable = (LayerDrawable) seekBar.getProgressDrawable(); 
Drawable filledDrawable = progressDrawable.findDrawableByLayerId(android.R.id.progress); 
filledDrawable.setColorFilter(ContextCompat.getColor(сontext, R.color.color_new), PorterDuff.Mode.SRC_IN); 
+0

我得到了類的演員這裏的例外 – CarinaMj

+0

seekbar.getProgressDrawable()返回'X extends Drawable'。你可以發佈返回的具體類嗎? – DeKaNszn

+0

@CarinaMj我已更改爲您的情況的簡單代碼 – DeKaNszn

相關問題