每当我想在我的应用程序中显示进度条时,我调用此方法,此方法将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);
}
}
答案 0 :(得分:5)
尝试这样的事情,它应该全局工作:
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);
}
}
答案 1 :(得分:1)
如果您正在为对话框使用自定义设计布局,则可以将进度条放在进度对话框自定义布局中并从那里使用它。然后,当您的对话框显示时,它将显示在您的对话框上方。
答案 2 :(得分:1)
尝试为对话框使用对话框片段,并在对话框片段的布局中膨胀进度条。有关详细信息,请查看此链接。
https://developer.android.com/reference/android/app/DialogFragment.html
答案 3 :(得分:1)
试试这个
ProgressDialog dialog = ProgressDialog.show(MyActivity.this, "",
"Loading. Please wait...", true);
这将为您提供一个进度条,可根据您的要求使用。
答案 4 :(得分:1)
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了解更多信息。
答案 5 :(得分:1)
我能想到的一件事是在视图中使用抽象的Dialog Fragment或普通的自定义Dialog以及进度条。话虽如此,您的类应该允许您附加对话框或其他视图。这样,每次你想显示对话框,扩展该类或使用该类并提供视图。
或者
再次在对话框中调用进度对话框。但我更喜欢第一个。答案 6 :(得分:1)
simpleProgressBar.setMax(100);
simpleProgressBar.setProgress(50);
答案 7 :(得分:-2)
使用以下功能代码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;
}