How to center progress indicator in ProgressDialog easily (when no title/text passed along)

后端 未结 8 886
死守一世寂寞
死守一世寂寞 2020-11-28 00:43

When calling progressDialog = ProgressDialog.show(this, null, null, true); usually the developers wants to only show the progress indication image, and usually

8条回答
  •  忘掉有多难
    2020-11-28 01:08

    I did some testing and I feel that the best way to achieve this is doing a custom 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.

    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 this link, nothing strange, but the magic occurs in the constructor. Check that I pass as parameter an style. That style is the following:

    
    
        
    
    

    The result of this is a ProgressBar rotating in the center of the screen. Without backgroundDim and without the Dialog box.

提交回复
热议问题