繁体   English   中英

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

[英]How to add progress bar over dialog

每当我想在我的应用程序中显示进度条时,我调用此方法,此方法将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);
    }

}

如果您正在为对话框使用自定义设计布局,则可以将进度条放在进度对话框自定义布局中并从那里使用它。 然后,当您显示对话框时,它将显示在您的对话框上方。

尝试使用对话框片段进行对话,并在对话框片段的布局中膨胀进度条。有关详细信息,请查看此链接。

https://developer.android.com/reference/android/app/DialogFragment.html

试试这个

ProgressDialog dialog = ProgressDialog.show(MyActivity.this, "", 
                        "Loading. Please wait...", true);

这将为您提供一个进度条,可根据您的要求使用。

Android有一个ProgressDialog,其中AlertDialog作为基类,并添加了一个进度功能:

  ProgressDialog progress = new ProgressDialog(this);
  progress.setMessage("Downloading Music");
  progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
  progress.setIndeterminate(true);
  progress.setProgress(0);
  progress.show();

和增量:

  progress.setProgress( ... );

点击这里查看更多。

我能想到的一件事是在视图中使用抽象的Dialog Fragment或普通的自定义Dialog和进度条。 话虽如此,您的类应该允许您附加对话框或其他视图。 这样,每次你想显示对话框,扩展该类或使用该类并提供视图。

要么

再次在对话框内调用进度对话框。 但我更喜欢第一个。

simpleProgressBar.setMax(100); 
simpleProgressBar.setProgress(50); 

使用以下功能代码进行ProgressBar

public void Progressbar_Timer() {
    final ProgressDialog dialog = new ProgressDialog(AddEventImageLayout.this);
    dialog.setTitle("Placeing selected image...");
    dialog.setMessage("Please wait..");
    dialog.setIndeterminate(true);
    dialog.setCancelable(false);
    dialog.show();

    long delayInMillis = 10000;
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            dialog.dismiss();
        }
    }, delayInMillis);
    check = 2;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM