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

Android 可绘制xml形状颜色动态变化,android,seekbar,xml-drawable,android-selector,Android,Seekbar,Xml Drawable,Android Selector,我使用的是SeekBar,我需要在列表中使用不同颜色的SeekBar 因此,我需要以编程方式更改可绘制形状的颜色 这是DrawaleXML。 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/

我使用的是
SeekBar
,我需要在列表中使用不同颜色的
SeekBar

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

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

我必须动态地改变这个绿色区域。为此,我在此xml中设置了颜色点_progress.xml

请提出这个想法 提前感谢……

使用此:

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);
使用以下命令:

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);

我在这里得到了类强制转换异常seekbar.getProgressDrawable()返回
X扩展Drawable
。你能为它发布返回的特定类吗?@CarinaMj我已经为你的情况更改为简单代码。如果我使用此代码,然后更改整个背景。但是,我只需要更改进展区域颜色的背景。背景应使用相同的。但所选区域应动态更改。。。请将更新后的问题与image@CarinaMj我添加了仅用于更改进展区域背景的示例。我在此处得到了类强制转换异常seekbar.getProgressDrawable()返回
X extends Drawable
。你能为它发布返回的特定类吗?@CarinaMj我已经为你的情况更改为简单代码。如果我使用此代码,然后更改整个背景。但是,我只需要更改进展区域颜色的背景。背景应使用相同的。但所选区域应动态更改。。。请将更新后的问题与image@CarinaMj我已经添加了一个示例,仅用于更改进展区域的背景。