Android 如何轻松地在 ProgressDialog 中居中进度指示器(当没有标题/文本传递时)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3225889/
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
How to center progress indicator in ProgressDialog easily (when no title/text passed along)
提问by Mathias Conradt
When calling progressDialog = ProgressDialog.show(this, null, null, true);
usually the developers wants to only show the progress indication image, and usually would it expect to be centered within the window (at least from regular UI design point of view).
But the image is too far left, it seems that some padding/margin on the right hand side is still being calculated in for (optional) text on the right, although we're not passing any text as parameter.
It would just make life little easier for a developer :) So we don't need to create a custom dialog only in order to have the progress indicator being centered by default.
在调用时progressDialog = ProgressDialog.show(this, null, null, true);
,开发人员通常只想显示进度指示图像,并且通常希望在窗口内居中(至少从常规 UI 设计的角度来看)。但是图像太靠左了,似乎右侧的一些填充/边距仍在为右侧的(可选)文本计算,尽管我们没有将任何文本作为参数传递。它只会让开发人员的生活变得更轻松 :) 所以我们不需要创建一个自定义对话框,只是为了让进度指示器默认居中。
(I filed this as a feature request at http://code.google.com/p/android/issues/detail?id=9697; please star it if you would also like to see this improved).
(我在http://code.google.com/p/android/issues/detail?id=9697将此作为功能请求提交;如果您也希望看到此改进,请加注星标)。
Now my questions:
现在我的问题:
How can I easily center the progress image without having to entirely create my own custom alert dialog class? Any parameter I might have overlooked?
Furthermore, how to set the background to transparent?
如何轻松地将进度图像居中,而不必完全创建我自己的自定义警报对话框类?我可能忽略了任何参数?
此外,如何将背景设置为透明?
I'm also wondering about this: https://stackoverflow.com/questions/2866141/how-to-put-custom-animation-into-a-progressdialogI haven't actually tried it myself yet but if you cannot create custom animations, it means if you want a kind of animated progress indicator, you always need to extend the ProgressDialog class? Looking at the ProgressDialog class though, I don't find anything other than regular drawables though (ProgressDialog.java), they're not using AnimatedDrawable there.
我也想知道这个:https: //stackoverflow.com/questions/2866141/how-to-put-custom-animation-into-a-progressdialog我还没有真正尝试过,但如果你不能创建自定义动画,这意味着如果你想要一种动画进度指示器,你总是需要扩展 ProgressDialog 类?尽管查看 ProgressDialog 类,但除了常规可绘制对象(ProgressDialog.java)之外,我没有找到任何其他内容,但它们没有在那里使用 AnimatedDrawable 。
回答by Macarse
I did some testing and I feel that the best way to achieve this is doing a custom Dialog
.
我做了一些测试,我觉得实现这一目标的最佳方法是自定义Dialog
.
Here is an example of what I did. This will answer question number 2 but will give you an idea of how to fix question number 1.
这是我所做的一个例子。这将回答问题 2,但会让您了解如何解决问题 1。
public class MyProgressDialog extends Dialog {
public static MyProgressDialog show(Context context, CharSequence title,
CharSequence message) {
return show(context, title, message, false);
}
public static MyProgressDialog show(Context context, CharSequence title,
CharSequence message, boolean indeterminate) {
return show(context, title, message, indeterminate, false, null);
}
public static MyProgressDialog show(Context context, CharSequence title,
CharSequence message, boolean indeterminate, boolean cancelable) {
return show(context, title, message, indeterminate, cancelable, null);
}
public static MyProgressDialog show(Context context, CharSequence title,
CharSequence message, boolean indeterminate,
boolean cancelable, OnCancelListener cancelListener) {
MyProgressDialog dialog = new MyProgressDialog(context);
dialog.setTitle(title);
dialog.setCancelable(cancelable);
dialog.setOnCancelListener(cancelListener);
/* The next line will add the ProgressBar to the dialog. */
dialog.addContentView(new ProgressBar(context), new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
dialog.show();
return dialog;
}
public MyProgressDialog(Context context) {
super(context, R.style.NewDialog);
}
}
All the static methods comes from thislink, nothing strange, but the magic occurs in the constructor. Check that I pass as parameter an style. That style is the following:
所有的静态方法都来自这个链接,没什么奇怪的,但神奇的是在构造函数中。检查我是否将样式作为参数传递。该样式如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="NewDialog" parent="@android:Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowTitleStyle">@null</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:background">@android:color/transparent</item>
</style>
</resources>
The result of this is a ProgressBar
rotating in the center of the screen. Without backgroundDim
and without the Dialog
box.
这样做的结果是ProgressBar
在屏幕中心旋转。有backgroundDim
和没有Dialog
盒子。
回答by Hpsaturn
Easy and customizable way:
简单且可定制的方式:
Define animation:(res/drawable/loader_anim.xml)
定义动画:(res/drawable/loader_anim.xml)
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/image_for_rotation"
android:pivotX="50%"
android:pivotY="50%" />
or:
或者:
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@drawable/img_loader_frame1"
android:duration="150"/>
...
<item
android:drawable="@drawable/img_loader_frame2"
android:duration="150"/>
...
</animation-list>
then, define layout:(res/layout/loader.xml)
然后,定义布局:(res/layout/loader.xml)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="center_vertical|center_horizontal">
<ProgressBar
android:layout_width="200dp"
android:layout_height="200dp"
android:indeterminateDrawable="@drawable/loader_anim" />
</LinearLayout>
and then, instance progress dialog:
然后,实例进度对话框:
ProgressDialog dialog;
...
dialog = ProgressDialog.show(this,null,null);
dialog.setContentView(R.layout.loader);
...
process();
...
dialog.dismiss();
More info:
更多信息:
回答by egalot
I use the following, it requires no layout file, and puts a centered, borderless blocking progress bar in the middle of the screen.
我使用以下,它不需要布局文件,并在屏幕中间放置一个居中、无边界的阻塞进度条。
private ProgressDialog progressDialog;
setUIToWait(true);
...long process...
setUIToWait(false);
private void setUIToWait(boolean wait) {
if (wait) {
progressDialog=ProgressDialog.show(this,null,null);
progressDialog.setContentView(new ProgressBar(this));
} else {
progressDialog.dismiss();
}
}
回答by Thafer Shahin
If you want to display indeterminate progress bar only.
如果您只想显示不确定的进度条。
ProgressDialog progressDialog = ProgressDialog.show(this, null, null, true, false);
progressDialog.setContentView(R.layout.progress_layout);
And create a layout xml file with name "progress_layout.xml"
并创建一个名为“progress_layout.xml”的布局xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</LinearLayout>
回答by Arun Mohan
Just Do The Below Method To Get It Done
只需执行以下方法即可完成
In res->values->styles add the below code
在 res->values->styles 添加以下代码
<style name="MyGravity" parent="android:Theme.Material.Dialog" ></style>
Then create yours ProgressDialog as mentioned below
然后创建你的 ProgressDialog 如下所述
ProgressDialog dialog = new ProgressDialog(ctx, R.style.MyGravity);
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
回答by Puthuumalar
//create Dialog by using below method
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG1_KEY: {
Dialog dialog = new Dialog(this,R.style.NewDialog);
dialog.setContentView(R.layout.progress);
dialog.setCancelable(true);
return dialog;
}
}
return null;
}
//then in your onCreate () you can call like below
@Override
public void onCreate(Bundle savedInstatncestate)
{
final Handler mHandler = new Handler();
showDialog(DIALOG1_KEY);
new Thread() {
public void run() {
try {
sleep(3000);
Runnable r = new Runnable()
{
public void run()
{
//do your background process
dismissDialog(DIALOG1_KEY);
}
};
mHandler.post(r);
} catch (Exception e) {
}
}
}.start();
}
回答by Rathore
You can always add one ProgressBar in all your activities where you might want to show centered ProgressDialog. Use following in acitivity.xml As:
您始终可以在您可能希望显示居中 ProgressDialog 的所有活动中添加一个 ProgressBar。在 acitivity.xml 中使用以下内容:
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar"
android:layout_centerInParent="true"
android:visibility="gone"/>
In your Acitivity.java, use
在您的 Acitivity.java 中,使用
ProgressBar bar = new Progress();
bar = (ProgressBar) findViewById(R.id.progressBar);
Now when you want to show the ProgressBar, just set its visibility to visible, and to cancel, set visibility to gone.
现在,当您想要显示 ProgressBar 时,只需将其可见性设置为可见,而要取消,将可见性设置为消失。
bar.setVisibility(View.VISIBLE);
bar.setVisibility(View.GONE);
回答by Nishant
Using ProgressBar and adding it to LinearLayout worked in my Case as follows:
使用 ProgressBar 并将其添加到 LinearLayout 在我的案例中工作如下:
ProgressBar mSpinner = new ProgressBar(this);
mSpinner.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
mSpinner.setBackgroundResource(R.drawable.loading_1);
mSpinner.setIndeterminate(true);