且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何动态地改变进度条的背景机器人的颜色

更新时间:2023-11-19 12:21:58

请参阅Android ?ProgressBar.setProgressDrawable只能一次的答案,重复如下:

See Android ProgressBar.setProgressDrawable only works once? for the answer, repeated below:

我发现的是,绘制不知道它的大小,当setprogressdrawable被调用。当最初设置,它不知道它的大小。这意味着有一个新的绘制设置为搜索栏,但绘制的大小为0,你不会看到任何东西。

What I found out is that the drawable doesn't know it's size when setprogressdrawable is called. When it is initially set up, it does know it's size. This means there is a new drawable set to the seekbar, but the size of the drawable is 0, you won't see anything.

解决的办法是先获取当前绘制的边界,然后设置新的绘制,最后再设定范围:

The solution is to first get the bounds of the current drawable, then set the new drawable and finally set the bounds again:

Rect bounds = mySeekBar.getProgressDrawable().getBounds();
mySeekBar.setProgressDrawable(newSeekBarBackground);
mySeekBar.getProgressDrawable().setBounds(bounds);