如何正确使用setProgressDrawable()?
我在为我的 ProgressBar 设置新的 Drawable 时遇到问题。
如果我在 onCreate() 方法中使用 setProgressDrawable() ,效果会很好。但是,当我尝试在处理程序后回调中调用相同的方法时,它不起作用并且进度条消失。
有人可以解释这种行为吗?我该如何解决这个问题?
I am having problem with setting a new Drawable to my ProgressBar.
If I use the setProgressDrawable() inside onCreate() method it works great. But when I try to call the same method inside a Handler post callback it doesn't work and the progressbar disapears.
Can someone explain this behaviour? How can I solve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我自己遇到了这个问题,我设法让它工作:)
我使用了
AsyncTask
来处理后台任务/线程,但其想法应该与使用Runnable/Handler
相同(尽管AsyncTask
确实感觉更好我认为)。所以,这就是我所做的...将
setContentView(R.layout.my_screen);
放入onPostExecute
方法中! (即,而不是onCreate
方法)所以代码看起来像这样:
说实话,为什么你需要在
onPostExecute
中调用setContentView
我不知道......但这样做意味着您可以为进度条设置自定义样式(并且它们不会在您身上消失!)Bumped into this problem myself and I managed to get it working :)
I used the
AsyncTask
to handle the background tasks/threads, but the idea should be the same as usingRunnable/Handler
(thoughAsyncTask
does feel nicer imo).So, this is what I did... put
setContentView(R.layout.my_screen);
in theonPostExecute
method! (ie. instead of theonCreate
method)So the code looks something like this:
To be honest, why you need to call
setContentView
inonPostExecute
I have no idea... but doing so means you can set custom styles for your progress bars (and they don't disappear on you!)也许您将代码放在非主线程的线程中。
如果您想使用 UI,则必须在主线程中执行此操作:)
Maybe you put the code in a thread which is not main thread.
If you want to work with the UI, you must do that in the main thread :)
我也面临同样的问题,但就我而言,这是由于使用了 Drawable.mutate() 方法。当我删除该方法时,它开始正常工作。我还注意到这个问题存在于 api level-21(lollipop) 以下。
I was also facing the same issue but in my case it is due to the use of Drawable.mutate() method. When i removed that method it started working fine. I also noticed that this issue exist below api level-21(lollipop).