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

前端之家收集整理的这篇文章主要介绍了android – 如何在对话框上添加进度条前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
每当我想在我的应用程序中显示进度条时,我调用方法,此方法将ProgressBar添加到我的布局中.

问题:我想在Dialog上显示此进度条,但Dialog始终显示在上面.这种情况可以做些什么?

  1. public static void showProgressBar(@NonNull final Activity activity) {
  2. try {
  3. if (activity == null) {
  4. LogManager.printStackTrace(new NullActivityException());
  5. return;
  6. }
  7. View view = activity.findViewById(android.R.id.content);
  8. if (view == null) {
  9. LogManager.printStackTrace(new NullPointerException("content view is null"));
  10. return;
  11. }
  12. View rootView = activity.findViewById(android.R.id.content).getRootView();
  13. if (rootView == null || !(rootView instanceof ViewGroup)) {
  14. LogManager.printStackTrace(new NullPointerException("rootView is null or not an instance of ViewGroup"));
  15. return;
  16. }
  17. final ViewGroup layout = (ViewGroup) rootView;
  18.  
  19. final ProgressBar progressBar = new ProgressBar(activity);
  20. progressBar.setIndeterminate(true);
  21. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
  22. Drawable wrapDrawable = DrawableCompat.wrap(progressBar.getIndeterminateDrawable());
  23. DrawableCompat.setTint(wrapDrawable,ContextCompat.getColor(activity,R.color.colorAccent));
  24. progressBar.setIndeterminateDrawable(DrawableCompat.unwrap(wrapDrawable));
  25. }
  26. RelativeLayout.LayoutParams params = new
  27. RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT);
  28. final RelativeLayout rl = new RelativeLayout(activity);
  29. rl.setBackgroundColor(ActivityCompat.getColor(activity,R.color.tc_hint_grey_alpha));
  30. rl.setClickable(true);
  31. rl.setTag("#$UniqueProgressBar");
  32. ViewGroup.LayoutParams params2 = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,120);
  33. rl.setGravity(Gravity.CENTER);
  34. rl.addView(progressBar,params2);
  35. LogManager.i("ProgressBar","ProgressUtils.showProgressBar->called");
  36. layout.addView(rl,params);
  37.  
  38. mRunnable = new Runnable() {
  39. @Override
  40. public void run() {
  41. LogManager.i("ProgressBar","ProgressUtils.showProgressBar->120 secs timeout");
  42. hideProgressBar(activity);
  43. }
  44. };
  45. mHandler = new Handler();
  46. mHandler.postDelayed(mRunnable,120000);
  47.  
  48. LogManager.i("ProgressBar","Added");
  49. } catch (Exception e) {
  50. LogManager.printStackTrace(e);
  51. }
  52.  
  53. }

解决方法

尝试这样的东西,它应该全局工作:
  1. public static void showProgressBar(@NonNull final Activity activity) {
  2. try {
  3. if (activity == null) {
  4. LogManager.printStackTrace(new NullActivityException());
  5. return;
  6. }
  7.  
  8. final WindowManager wm = (WindowManager) activity.getApplicationContext().getSystemService(Activity.WINDOW_SERVICE);
  9.  
  10. final ProgressBar progressBar = new ProgressBar(activity);
  11. progressBar.setIndeterminate(true);
  12. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
  13. Drawable wrapDrawable = DrawableCompat.wrap(progressBar.getIndeterminateDrawable());
  14. DrawableCompat.setTint(wrapDrawable,R.color.colorAccent));
  15. progressBar.setIndeterminateDrawable(DrawableCompat.unwrap(wrapDrawable));
  16. }
  17.  
  18. WindowManager.LayoutParams windowLayoutParams = new WindowManager.LayoutParams();
  19. windowLayoutParams.gravity = Gravity.CENTER;
  20. windowLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_PANEL;
  21. windowLayoutParams.token = activity.getWindow().getDecorView().getWindowToken();
  22. windowLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
  23.  
  24. windowLayoutParams.height = LayoutParams.MATCH_PARENT;
  25. windowLayoutParams.width = LayoutParams.MATCH_PARENT;
  26.  
  27. wm.addView(progressBar,windowLayoutParams);
  28.  
  29. LogManager.i("ProgressBar","Added");
  30. } catch (Exception e) {
  31. LogManager.printStackTrace(e);
  32. }
  33.  
  34. }

猜你在找的Android相关文章