Android - 在没有对话框的情况下显示不确定的进度条
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/704295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - show an indeterminate progressbar without the dialog
提问by lostInTransit
I want to show my progressbar in the center of the screen when a processing happens on a button click. But I just want the progressbar without the dialog box..
当单击按钮时发生处理时,我想在屏幕中央显示我的进度条。但我只想要没有对话框的进度条..
Is there any way I can do this?
有什么办法可以做到这一点吗?
回答by reflog
It's explained in full in ApiDemos inside the SDK. The example that you want is named: ProgressBar3.java and can be found in \ApiDemos\src\com\example\android\apis\view\
它在 SDK 内的 ApiDemos 中有完整的解释。您想要的示例名为:ProgressBar3.java,可以在 \ApiDemos\src\com\example\android\apis\view\ 中找到
Also, if you want to remove the borders of the dialog so that only the progress bar appears you can define the dialog itself as a transparent view/overlay (it's explained in the examples as well).
此外,如果您想删除对话框的边框以便仅显示进度条,您可以将对话框本身定义为透明视图/覆盖(示例中也有说明)。
回答by Jesse Finnerty
If you need no dialog, just put the progressBar in your layout like any other view item, and set the visibility to 'INVISIBLE' or even 'GONE' by default.
如果您不需要对话框,只需将 progressBar 像任何其他视图项一样放在您的布局中,并将可见性设置为“INVISIBLE”甚至默认设置为“GONE”。
Then just show it whenever you need to by changing it to 'VISIBLE' using setVisibility in your code, and re-hide it when the task is done by making it 'INVISIBLE' or 'GONE' again.
然后只需在需要时通过在代码中使用 setVisibility 将其更改为“VISIBLE”来显示它,并在任务完成时通过再次将其设置为“INVISIBLE”或“GONE”来重新隐藏它。
e.g.
例如
MyTask {
// get handle on ProgressBar ViewItem defined in XML (id:progressBar)
ProgressBar progressBar = findViewById(R.id.progressBar);
//starting task, show progress bar
progressBar.setVisibility(View.VISIBLE);
// Do some stuff...
//task done, hide it again
progressBar.setVisibility(View.GONE);
}
This is a simple version, all done in the UI thread, but this should be easy to adapt if you are using an asynchronous task or thread handlers. Just show, hide, and post any updates to the dialog while you are in the UI thread, and do your long-running task in the background.
这是一个简单的版本,全部在 UI 线程中完成,但如果您使用异步任务或线程处理程序,这应该很容易适应。只需在 UI 线程中显示、隐藏和发布对对话框的任何更新,并在后台执行长时间运行的任务即可。
回答by Yogesh
@reflog: That's not what is needed I guess.
@reflog:我猜这不是需要的。
@lostInTransit:
@lostInTransit:
The correct way to achieve this is as follows:
实现这一目标的正确方法如下:
- Create ur own dialog
- Put progress bar as only view on the dialog
- Set dialog window background transparent
- Show the dialog.
- 创建你自己的对话框
- 将进度条作为对话框中的唯一视图
- 设置对话窗口背景透明
- 显示对话框。
That's it. It works for me on devices with 1.5 to 2.1. Not tried on 2.2.
就是这样。它适用于 1.5 到 2.1 的设备。2.2没试过。
-Yogesh
-约格什
回答by WBAN
The answer by @Jesse Finnerty solved my issue. what i did was:
@Jesse Finnerty 的回答解决了我的问题。我所做的是:
private ProgressBar mprogressBar;
The inside oncreate() just like other buttons and textviews i did, mprogressBar = (ProgressBar) findViewById(R.id.progressBar);
内部的 oncreate() 就像我所做的其他按钮和文本视图一样, mprogressBar = (ProgressBar) findViewById(R.id.progressBar);
Then at the point i want spinning wheel to appear i give command
然后在我希望纺车出现的时候我发出命令
mprogressBar.setVisibility(View.VISIBLE);
at the point where i want it to disappear .
在我希望它消失的时候。
mprogressBar.setVisibility(View.INVISIBLE);
This is under switch actually. so that as the states in my program change, the progress bar appears and disappears
这实际上是在 switch 中。这样当我的程序中的状态发生变化时,进度条就会出现和消失
回答by WhoDatSaint
There is a great example to do what Yogesh is suggesting at:
有一个很好的例子来做 Yogesh 的建议:
How to center progress indicator in ProgressDialog easily (when no title/text passed along)
如何轻松地在 ProgressDialog 中居中进度指示器(当没有标题/文本传递时)
Credit goes to Macarse for posting it.
归功于 Macarse 发布它。