搜索网站后,我找到了以下代码来更改进度条的颜色。这在我第一次调用代码时工作正常(进度条变为绿色),但是每次调用代码后我都会得到一个空白进度条。有没有人遇到过这个问题?如果是这样,每次调用“setProgressDrawable”时能够更改进度条颜色的解决方案是什么?
<?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="5dip" />
<gradient
android:startColor="#ff9d9e9d"
android:centerColor="#ff5a5d5a"
android:centerY="0.75"
android:endColor="#ff747674"
android:angle="270"
/>
</shape>
</item>
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#80ffd300"
android:centerColor="#80ffb600"
android:centerY="0.75"
android:endColor="#a0ffcb00"
android:angle="270"
/>
</shape>
</clip>
</item>
<item
android:id="@android:id/progress">
<clip>
<shape>
<corners
android:radius="5dip" />
<gradient
android:startColor="@color/greenStart"
android:centerColor="@color/greenMid"
android:centerY="0.75"
android:endColor="@color/greenEnd"
android:angle="270"
/>
</shape>
</clip>
</item>
</layer-list>
在 strings.xml 文件中定义颜色
<color name="greenStart">#ff33dd44</color>
<color name="greenMid">#ff0A8815</color>
<color name="greenEnd">#ff1da130</color>
改变颜色的代码
Rect bounds = bar.getProgressDrawable().getBounds();
bar.setProgressDrawable(getResources().getDrawable(R.drawable.green_progress));
bar.getProgressDrawable().setBounds(bounds);
最佳答案
我终于找到了解决问题的方法。如果我要更改的颜色与已设置的颜色相同,则进度条将变为空白。因此,在我上面的代码中,进度条第一次从默认的黄色变为绿色。然而,下一次调用代码时,进度条(已经设置为绿色)再次设置为绿色,结果整个进度条将变为空白。
为了解决这个问题,我记录了进度条当前显示的颜色,当我再次设置颜色时,我只在它与当前显示的颜色不同时才进行设置。
希望这对遇到同样情况的其他人有所帮助。
关于android - 更改进度条颜色导致进度条不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7845108/