更改进度条颜色导致进度条不显示
搜索网站后,我发现下面的代码可以更改进度条的颜色。我第一次调用代码时效果很好(进度条变为绿色),但是每次调用代码后我都会得到一个空白的进度条。以前有人遇到过这个问题吗?如果是这样,每次调用“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);
After searching the site I've found the below code to change the color of my progressbar. This works fine the first time I call the code (progressbar changes to green), however each time after I call the code I get a blank progressbar. Has anyone faced this issue before? If so, what was the solution to be able to change the progressbar color each time 'setProgressDrawable' is called?
<?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>
Define colors in strings.xml file
<color name="greenStart">#ff33dd44</color>
<color name="greenMid">#ff0A8815</color>
<color name="greenEnd">#ff1da130</color>
Code to change color
Rect bounds = bar.getProgressDrawable().getBounds();
bar.setProgressDrawable(getResources().getDrawable(R.drawable.green_progress));
bar.getProgressDrawable().setBounds(bounds);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我终于找到了解决我的问题的方法。如果我要更改的颜色与已设置的颜色相同,进度条将变为空白。因此,使用上面的代码,第一次进度条将从默认的黄色变为绿色。然而,下次调用代码时,进度条(已设置为绿色)再次设置为绿色,结果整个进度条将变为空白。
为了解决这个问题,我记录了进度条当前显示的颜色,当我再次设置颜色时,我仅在它与当前显示的颜色不同时才设置它。
希望这可以帮助其他遇到同样情况的人。
I finally found a solution to my problem. The progressbar would become blank if the color I was changing to was the same color that was already set. So with my code above the first time the progressbar would change from the default yellow to green. However, the next time the code was called the progressbar (which was already set to green) was agin set to green and as a result the entire progressbar would become blank.
To get around this I've recorded the currently displayed color of the progressbar and when I get around to setting the color again I only set it if it is different then the one currently displayed.
Hope this helps others who find themselves in the same situation.