android - 如何在对话框上添加进度条

标签 android dialog android-progressbar

每当我想在我的应用程序中显示进度条时,我都会调用此方法,此方法会将 ProgressBar 添加到我的布局中。

问题: 我想在 Dialog 上显示这个进度条,但 Dialog 总是显示在上方。对于这种情况可以做些什么?

public static void showProgressBar(@NonNull final Activity activity) {
    try {
        if (activity == null) {
            LogManager.printStackTrace(new NullActivityException());
            return;
        }
        View view = activity.findViewById(android.R.id.content);
        if (view == null) {
            LogManager.printStackTrace(new NullPointerException("content view is null"));
            return;
        }
        View rootView = activity.findViewById(android.R.id.content).getRootView();
        if (rootView == null || !(rootView instanceof ViewGroup)) {
            LogManager.printStackTrace(new NullPointerException("rootView is null or not an instance of ViewGroup"));
            return;
        }
        final ViewGroup layout = (ViewGroup) rootView;

        final ProgressBar progressBar = new ProgressBar(activity);
        progressBar.setIndeterminate(true);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            Drawable wrapDrawable = DrawableCompat.wrap(progressBar.getIndeterminateDrawable());
            DrawableCompat.setTint(wrapDrawable, ContextCompat.getColor(activity, R.color.colorAccent));
            progressBar.setIndeterminateDrawable(DrawableCompat.unwrap(wrapDrawable));
        }
        RelativeLayout.LayoutParams params = new
                RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.MATCH_PARENT);
        final RelativeLayout rl = new RelativeLayout(activity);
        rl.setBackgroundColor(ActivityCompat.getColor(activity, R.color.tc_hint_grey_alpha));
        rl.setClickable(true);
        rl.setTag("#$UniqueProgressBar");
        ViewGroup.LayoutParams params2 = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                120);
        rl.setGravity(Gravity.CENTER);
        rl.addView(progressBar, params2);
        LogManager.i("ProgressBar", "ProgressUtils.showProgressBar->called");
        layout.addView(rl, params);

        mRunnable = new Runnable() {
            @Override
            public void run() {
                LogManager.i("ProgressBar", "ProgressUtils.showProgressBar->120 secs timeout");
                hideProgressBar(activity);
            }
        };
        mHandler = new Handler();
        mHandler.postDelayed(mRunnable, 120000);

        LogManager.i("ProgressBar", "Added");
    } catch (Exception e) {
        LogManager.printStackTrace(e);
    }

}

最佳答案

尝试这样的事情,它应该在全局范围内工作:

public static void showProgressBar(@NonNull final Activity activity) {
    try {
        if (activity == null) {
            LogManager.printStackTrace(new NullActivityException());
            return;
        }

        final WindowManager wm = (WindowManager) activity.getApplicationContext().getSystemService(Activity.WINDOW_SERVICE);

        final ProgressBar progressBar = new ProgressBar(activity);
        progressBar.setIndeterminate(true);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            Drawable wrapDrawable = DrawableCompat.wrap(progressBar.getIndeterminateDrawable());
            DrawableCompat.setTint(wrapDrawable, ContextCompat.getColor(activity, R.color.colorAccent));
            progressBar.setIndeterminateDrawable(DrawableCompat.unwrap(wrapDrawable));
        }

        WindowManager.LayoutParams windowLayoutParams = new WindowManager.LayoutParams();
                windowLayoutParams.gravity = Gravity.CENTER;
                windowLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_PANEL;
                windowLayoutParams.token = activity.getWindow().getDecorView().getWindowToken();
                windowLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;

        windowLayoutParams.height = LayoutParams.MATCH_PARENT;
        windowLayoutParams.width = LayoutParams.MATCH_PARENT;

        wm.addView(progressBar, windowLayoutParams);

        LogManager.i("ProgressBar", "ProgressUtils.showProgressBar->called");
        layout.addView(rl, params);

        mRunnable = new Runnable() {
            @Override
            public void run() {
                LogManager.i("ProgressBar", "ProgressUtils.showProgressBar->120 secs timeout");
                hideProgressBar(activity);
            }
        };
        mHandler = new Handler();
        mHandler.postDelayed(mRunnable, 120000);

        LogManager.i("ProgressBar", "Added");
    } catch (Exception e) {
        LogManager.printStackTrace(e);
    }

}

关于android - 如何在对话框上添加进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45835794/

相关文章:

vba - 使用内置对话框时出错 "xldialogApplyNames"

java - 仅用作对话框窗口的 Android 应用程序

android - 按钮中间的进度条

android - ProgressBar 高度=match_parent

安卓 : Semi Circle Progress Bar

android - 从 uri 播放音频

java - 如何通过 GreenDAO 建立双向一对一关系?

python - 如何在没有命令提示符窗口的情况下运行 wxPython 对话框?

java - 以编程方式访问资源名称

java - Android Recyclerview 滚动更新日历