微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

每当我想在我的应用程序中显示进度条时,我调用方法,此方法将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,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","Added");
    } catch (Exception e) {
        LogManager.printstacktrace(e);
    }

}

原文地址:https://www.jb51.cc/android/318110.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐