33

安卓 2.3.3

我有一个进度对话框,显示正在加载.. 作为文本。这是进度对话框的代码。

progressDialog = new ProgressDialog(mContext);      
            progressDialog.setIndeterminate(true);
            progressDialog.setMessage("Loading...");
            progressDialog.show();

如果我删除 line progressDialog.setMessage("Loading...");,我会得到一个左侧的进度对话框和右侧的一个空框,它占据了父级的宽度。

我只想显示在中心对齐的 progressdialog 。请参考下面的图片..

这就是我所拥有的...

在此处输入图像描述

这就是我要的...

在此处输入图像描述

有人可以帮我弄这个吗?

4

13 回答 13

74

试试这个 1.create 一个这样的方法:

public static ProgressDialog createProgressDialog(Context context) {
    ProgressDialog dialog = new ProgressDialog(context);
    try {
        dialog.show();
    } catch (BadTokenException e) {

    }
    dialog.setCancelable(false);
    dialog.getWindow()
        .setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
    dialog.setContentView(R.layout.progressdialog);
    // dialog.setMessage(Message);
    return dialog;
}

// XML 布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@android:color/transparent" >

    <ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

</RelativeLayout>

并在任何你想要的地方调用这个方法:

if (progressDialog == null) {
    progressDialog = Utils.createProgressDialog(Login.this);
    progressDialog.show();
} else {
    progressDialog.show();
}
于 2013-06-07T09:32:23.213 回答
23

如果您碰巧收到错误:“ requestFeature() must be called before adding content”,解决方案是progressDialog.show()在您调用之前先调用progressDialog.setContentView(R.layout.progressdialog)

于 2014-08-05T16:54:43.500 回答
8

在带有强调色的材料设计上工作和看起来很好(如果你有的话)

努力使它适用于 5.0+ 和更低的 API 版本。解决方案是通过对话框而不是进度来实现。

在布局文件夹中创建一个文件aux_progress_spinner.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:background="@android:color/transparent"
              android:gravity="center"
              android:orientation="vertical" >

    <ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@android:color/transparent"
        />

</LinearLayout>

在某处创建一个函数......让我们说Utils.class

   public static Dialog LoadingSpinner(Context mContext){
        Dialog pd = new Dialog(mContext, android.R.style.Theme_Black);
        View view = LayoutInflater.from(mContext).inflate(R.layout.aux_progress_spinner, null);
        pd.requestWindowFeature(Window.FEATURE_NO_TITLE);
        pd.getWindow().setBackgroundDrawableResource(R.color.transparent);
        pd.setContentView(view);
        return pd;
    }

在您的片段/活动调用中:

 private Dialog progress_spinner;
 progress_spinner = Utils.LoadingSpinner(mContext);   

 //------ Where you want it ------
 progress_spinner.show();

 //------- Dismiss it --------
 progress_spinner.dismiss();

可选:添加一个简单的 CountDownTimer 来测试它的外观

        new CountDownTimer(1000,1000){

            @Override public void onTick(long millisUntilFinished) {

            }

            @Override public void onFinish() {
                progress.dismiss();
            }
        }.start();
于 2015-07-28T10:30:36.343 回答
7

您可以在 style.xml 中添加以下样式

<style name="progress_bar_style">
    <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>

在 color.xml 文件中添加任何颜色。

于 2013-06-07T09:29:54.280 回答
5

您可以使用:

Progress progress = new ProgressDialog(getActivity(), android.support.v4.app.DialogFragment.STYLE_NO_TITLE);

如有必要,添加到 Gradle 依赖项

compile 'com.android.support:support-v4:22.2.0'
于 2015-07-12T22:38:27.577 回答
1

使用 ProgressBar 而不是 ProgressDialog。 http://developer.android.com/reference/android/widget/ProgressBar.html

于 2013-06-07T09:14:15.867 回答
1

您可以使用 :

progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);

如果你不想要标题,你可以:

progressDialog.setTitle(null);
于 2013-09-20T10:06:02.233 回答
1

MuraliGanesan 的回答是正确的,但我只使用 Dialog,而不是 ProgressDialog 以避免“添加内容之前必须调用 requestFeature()”异常

于 2015-07-06T05:17:40.640 回答
1

基于 MuraliGanesan 的回答,我创建了一个progressDialog 的子类。

public class LoadingDialog extends ProgressDialog {

    public LoadingDialog(Context context) {
        super(context);
    }

    @Override
    public void show() {
        super.show();
        setCancelable(false);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setContentView(R.layout.custom_progress_dialog);
    }
}

您还必须在 res/layout 中创建 custom_progress_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    android:layout_gravity="center">

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

</RelativeLayout>

你可以像这样简单地使用它:

LoadingDialog dialog = new LoadingDialog(this);
dialog.show();
:
:
dialog.dismiss();
于 2017-07-25T20:38:16.607 回答
1

您可以通过创建一个扩展 Dialog 类的类来做到这一点:

假设我创建了以下类:

public class TransparentProgressDialog extends Dialog {

    public ImageView iv;

    public TransparentProgressDialog(Context context, int resourceIdOfImage) {
        super(context, R.style.TransparentProgressDialog);
        WindowManager.LayoutParams wlmp = getWindow().getAttributes();
        wlmp.gravity = Gravity.CENTER_HORIZONTAL;
        getWindow().setAttributes(wlmp);
        setTitle(null);
        setCancelable(false);
        setOnCancelListener(null);
        LinearLayout layout = new LinearLayout(context);
        layout.setOrientation(LinearLayout.VERTICAL);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        iv = new ImageView(context);
        iv.setImageResource(resourceIdOfImage);
        layout.addView(iv, params);
        addContentView(layout, params);
    }

    @Override
    public void show() {
        super.show();
        RotateAnimation anim = new RotateAnimation(0.0f, 360.0f , Animation.RELATIVE_TO_SELF, .5f, Animation.RELATIVE_TO_SELF, .5f);
        anim.setInterpolator(new LinearInterpolator());
        anim.setRepeatCount(Animation.INFINITE);
        anim.setDuration(3000);
        iv.setAnimation(anim);
        iv.startAnimation(anim);
    }
}

放入TransparentProgressDialogstyle.xml:

@null @android:color/transparent true @null @null @android:style/Animation.Dialog stateUnspecified|adjustPan true @android:color/transparent

现在您可以放置​​任何将传播的进度图像:

TransparentProgressDialog pDialog = new TransparentProgressDialog(mContext, R.drawable.progress);
pDialog.show();

而已。

于 2016-03-02T05:57:32.160 回答
0

这在 MonoDroid 中为我工作:

progressDialog.SetMessage((string)null);

在java中试试这个:

progressDialog.setMessage(null);
于 2014-03-20T21:30:43.920 回答
0

我所做的是..

private ProgressDialog progressDialog;

private void loadProgressDialog() {
    try {
        progressDialog = new ProgressDialog(this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setMessage("Please Wait...");
        progressDialog.setIndeterminate(false);
        progressDialog.setCancelable(false);
        progressDialog.setCanceledOnTouchOutside(false);
    } catch (Exception e) {
        e.printStackTrace();
    }
}


private void showProgressDialog() {
    if (progressDialog != null && !progressDialog.isShowing()) {
        progressDialog.show();
    }
}


private void hideProgressDialog() {
    if (progressDialog != null && progressDialog.isShowing()) {
        progressDialog.dismiss();
    }
}

输出

于 2020-05-24T16:27:06.650 回答
0
ProgressDialog.show(this, null,"", true);
于 2017-07-19T21:32:07.810 回答