可绘制的xml形状颜色变化动态

时间:2017-07-10 09:21:07

标签: android seekbar xml-drawable android-selector

我正在使用SeekBar,我需要在列表中使用不同颜色SeekBar的进度。

所以我需要以编程方式更改可绘制的形状颜色

这是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

我必须动态更改此绿色区域。为此,我在此xml中设置了颜色points_progress.xml

请提出想法 提前谢谢....

1 个答案:

答案 0 :(得分: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);
相关问题