如何轻松地在ProgressDialog中居中进度指示器(当没有标题/文本传递时)

Mat*_*adt 62 android progressdialog

progressDialog = ProgressDialog.show(this, null, null, true);通常在调用时,开发人员只想显示进度指示图像,并且通常希望在窗口中居中(至少从常规UI设计的角度来看).但是图像太远了,看起来右边的一些填充/边距仍然在右边的(可选)文本中计算,尽管我们没有传递任何文本作为参数.它只会让开发人员的生活变得更容易:)所以我们不需要创建自定义对话框,以便默认情况下使进度指示器居中.

(我在http://code.google.com/p/android/issues/detail?id=9697上将此作为功能请求提交;如果您还希望看到此改进,请将其加注星标).

现在我的问题:

  1. 如何轻松地将进度图像居中,而无需完全创建自己的自定义警报对话框类?我可能忽略了任何参数?

  2. 此外,如何将背景设置为透明?

我也想知道这个:https: //stackoverflow.com/questions/2866141/how-to-put-custom-animation-into-a-progressdialog 我还没有亲自尝试过但是如果你不能创建自定义动画,这意味着如果你想要一种动画进度指示器,你总是需要扩展ProgressDialog类吗?但是看看ProgressDialog类,我找不到除了常规drawable之外的任何东西(ProgressDialog.java),他们没有在那里使用AnimatedDrawable.

Mac*_*rse 104

我做了一些测试,我觉得实现这个目标的最好方法是做一个自定义Dialog.

这是我所做的一个例子.这将回答问题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);
    }
}
Run Code Online (Sandbox Code Playgroud)

所有的静态方法都来自这个链接,没什么奇怪的,但是魔法发生在构造函数中.检查我作为参数传递样式.那种风​​格如下:

<?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>
Run Code Online (Sandbox Code Playgroud)

结果是ProgressBar在屏幕中央旋转.没有backgroundDim和没有Dialog盒子.

  • 声明样式时请添加父项.这种风格可以在新的apis中产生很多问题:parent ="@ android:style/Theme.Dialog" (7认同)
  • 谢谢,很好地工作.我把它包含在我的应用程序中. (4认同)
  • @ Millec8 - 为了关闭对话框,请执行以下操作 - MyProgressDialog progressDialog; progressDialog = MyProgressDialog.show(ItemListActivity.this,"title","message"); progressDialog.dismiss(); (4认同)
  • @jul我也很难解雇自定义对话框,你们中的任何一个都可以解释一下你是如何静态调用它的吗?谢谢 (3认同)

Hps*_*urn 32

简单且可定制的方式:

定义动画: (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%" />
Run Code Online (Sandbox Code Playgroud)

要么:

<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>
Run Code Online (Sandbox Code Playgroud)

然后,定义布局: (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>
Run Code Online (Sandbox Code Playgroud)

然后,实例进度对话框:

ProgressDialog dialog;
...
dialog = ProgressDialog.show(this,null,null);
dialog.setContentView(R.layout.loader);
...
process();
...
dialog.dismiss();
Run Code Online (Sandbox Code Playgroud)

更多信息:


ega*_*lot 24

我使用以下内容,它不需要布局文件,并在屏幕中间放置一个居中的无边框阻止进度条.

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();
    }

}
Run Code Online (Sandbox Code Playgroud)


Tha*_*hin 19

如果您只想显示不确定的进度条.

ProgressDialog progressDialog = ProgressDialog.show(this, null, null, true, false);
progressDialog.setContentView(R.layout.progress_layout);
Run Code Online (Sandbox Code Playgroud)

并创建一个名为"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>
Run Code Online (Sandbox Code Playgroud)


小智 5

只需执行以下方法即可完成

在 res->values->styles 添加以下代码

<style name="MyGravity" parent="android:Theme.Material.Dialog" ></style>
Run Code Online (Sandbox Code Playgroud)

然后创建你的 ProgressDialog 如下所述

ProgressDialog dialog = new ProgressDialog(ctx, R.style.MyGravity);
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
Run Code Online (Sandbox Code Playgroud)