在android中更改进度条背景的颜色

在android中更改进度条背景的颜色,android,xml,progress-bar,Android,Xml,Progress Bar,我有一个Progressbar,它的外观在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"> <shape> <corne

我有一个Progressbar,它的外观在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">
        <shape>
            <corners  android:radius="@dimen/round_corners_elements" />
            <solid android:color="@color/blue_50" />

            <stroke android:width="1px" android:color="@color/light_gray" />
        </shape>
    </item>

    <item android:id="@android:id/progress">
        <clip android:clipOrientation="horizontal" android:gravity="left">
            <shape>
                <corners
                    android:radius="@dimen/round_corners_elements"
                   />
                <solid android:color="@color/signaling_color" />
                <stroke android:width="1px" android:color="@color/light_gray" />
            </shape>
        </clip>
    </item>

</layer-list>
setBackgroundColor也没有效果:


有人有主意了吗?

您必须获得
ProgressBar使用的

  • 用于获取该层的
    可绘制
    ,并在运行时对其进行修改
  • 创建一个新的
    Drawable
    并调用

如果我没记错的话,
标记会导致系统在运行时创建一个,这样可以方便地将类似

的方法更改为color.xml中定义的任何其他颜色。您是否将此xml drawable设置为
progressDrawable
属性?是的,它在Layouter中的progressDrawable标记中设置为drawable。我需要以编程方式设置它。我不帮助我在编译时设置它,这就解决了问题。我使用了第一个版本的运行时。
int[][] states = new int[][] { new int[] { android.R.attr.background }, new int[] { -android.R.attr.process } };
int[] colors = new int[] { Color.parseColor("#000000"), Color.BLACK };
ColorStateList myList = new ColorStateList(states, colors);
progressBar.setBackgroundTintList(myList);
progressBar.setBackgroundTintMode(PorterDuff.Mode.SRC_OVER);
 progressBar.setBackgroundColor(ContextCompat.getColor(this, R.color.white));
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        progressBar.setProgressBackgroundTintList(ColorStateList.valueOf(tint));
    } else {
        LayerDrawable drawable = (LayerDrawable) progressBar.getProgressDrawable();
        Drawable background = new ColorDrawable(tint);
        drawable.setDrawableByLayerId(android.R.id.background, background);
        progressBar.setProgressDrawable(drawable);
    }