【问题标题】:How to add progress bar over dialog如何在对话框上添加进度条
【发布时间】:2017-08-23 09:27:19
【问题描述】:

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

问题:我想在对话框上显示这个进度条,但对话框总是显示在上面。遇到这种情况该怎么办?

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

}

【问题讨论】:

  • 请发表评论,为什么要投反对票?
  • 为什么需要添加它?您可以显示一个并关闭另一个。反之亦然
  • @jiteshmohite 我在整个项目中都在使用它,而且我也有一些自定义设计对话框。这就是为什么需要它。
  • @jiteshmohite 我认为这是一个有效的要求。
  • 您可以在对话框布局中添加进度条作为组件。

标签: android dialog android-progressbar


【解决方案1】:

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

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

}

【讨论】:

    【解决方案2】:

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

    查看here 了解更多信息。

    【讨论】:

    • 请注意,这使用了一个 ProgressDialog,它刚刚在 API 26 中被弃用。
    【解决方案3】:

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

    【讨论】:

    • 但我需要为项目中使用的所有对话框提供全局解决方案。如果我在它们的布局中添加进度条,那将是太多的工作。
    • “全球解决方案”很糟糕。可重复使用的解决方案很好。 “工作太多”是懒惰,懒惰也是坏事。
    • 然后您可以创建一个基础对话框 Activity 或 Fragment,您的自定义 AlertDialog 必须扩展它以便在所有不同类型的对话框屏幕中显示进度条。
    【解决方案4】:

    尝试为您的对话框使用对话框片段并在对话框片段的布局内填充进度条。有关更多信息,请查看此链接。

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

    【讨论】:

      【解决方案5】:

      试试这个

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

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

      【讨论】:

      • ProgressDialog 自 API 26 以来已被弃用
      【解决方案6】:

      我能想到的一件事是在视图中有抽象的对话框片段或带有进度条的普通自定义对话框。话虽如此,您的课程应该使您能够附加对话框或其他视图。这样,每次您想显示对话框时,扩展该类或使用该类并提供视图。

      或者

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

      【讨论】:

        【解决方案7】:
        simpleProgressBar.setMax(100); 
        simpleProgressBar.setProgress(50); 
        

        【讨论】:

          【解决方案8】:

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

          【讨论】:

          • 这不是进度条,是进度对话框。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-07-07
          • 2011-05-11
          • 1970-01-01
          • 1970-01-01
          • 2022-12-05
          • 2019-01-30
          • 1970-01-01
          相关资源
          最近更新 更多