3

如何在不阻止点击屏幕上其他 UI 元素(如广告)的情况下在 Android 上显示进度对话框?

4

6 回答 6

3
public void addProgressBar(Activity activity){
    final ViewGroup rootFrameLayout = (ViewGroup) activity.getWindow().peekDecorView();
    final ViewGroup modal = new RelativeLayout(activity);
    ProgressBar progressBar = new ProgressBar(activity);
    LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
    modal.addView(progressBar, layoutParams);
    rootFrameLayout.addView(modal, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
    rootFrameLayout.invalidate();
}
于 2013-08-29T05:57:39.720 回答
1

您可以使用in创建完全transparent线性或任何其他类型的布局,并将此布局添加到包含所有其他控件的布局中。现在,当您想显示时,请制作此内容,并在您不想显示时制作。ProgressBarcenterFrameLayoutprogress barlayout visibleinvisible

于 2013-08-29T05:50:36.500 回答
1

您必须使用 ProgressBar 视图,并为进度条创建单独的叠加层。最透明的方法是创建一个抽象的 Activity 并覆盖 setContentView() 方法(都带有 id 和 View,仅带有 id 显示)。您可以从这个继承您的活动,并将其用作普通活动,并具有 showProgress 和 hideProgress 方法的额外好处。

private HashMap<View, ProgressBar> progressBars = new HashMap<View, ProgressBar>();
private RelativeLayout overlay;

public void setContentView(int id) {
   FrameLayout combinedView = new FrameLayout();
   combinedView.setLayoutParams(new ViewGroup.LayoutParams(
   LayoutParams.MATCH_PARENT,
   LayoutParams.MATCH_PARENT));
   View background = getLayoutInflater().inflate(id, null);
   background.setLayoutParams(new ViewGroup.LayoutParams(
   LayoutParams.MATCH_PARENT,
   LayoutParams.MATCH_PARENT));
   combinedView.addView(background);
   overlay = new RelativeLayout();
   overlay.setLayoutParams(new ViewGroup.LayoutParams(
   LayoutParams.MATCH_PARENT,
   LayoutParams.MATCH_PARENT));
   combinedView.addView(overlay);
   super.setContentView(combinedView);
}

public void showProgress(View view) {
   ProgressBar progressBar = progressBars.get(view);
   if (progressBar == null) {
      progressBar = new ProgressBar();
      progressBars.put(view, progressBar);
      progressBar.setLayoutParams(new ViewGroup.LayoutParams(
      LayoutParams.WRAP_CONTENT,
      LayoutParams.WRAP_CONTENT));
      overlay.addView(progressBar);
   }
   int[] position = view.getLocationOnScreen();
   int width = view.getWidth();
   int height = view.getHeight();
   int progressWidth = progressBar.getWidth();
   int progressHeight =progressBar.getHeight(); 
   progressBar.setMargins(position[0] + width / 2 - progressWidth/2,
   position[1] + height / 2 - progressHeight/2,0,0);
   progressBar.setVisibility(View.VISIBLE);
}

public void hideProgress(View view) {
   ProgressBar progressBar = progressBars.get(view);
   if (progressBar != null) {
      progressBar.setVisibility(View.INVISIBLE);
   }
}
于 2013-08-29T06:17:19.567 回答
1

bankings method I felt was the best and the simplest to implement. Just for the convenience of anybody wanting to use it, rather make the method return ProgressBar and when you want to remove this progress bar, you can use this return value to just set its visibility to gone as shown for example

progressBar.setVisibility(View.GONE);

Hope it adds value to the above answers.

于 2016-11-18T08:44:01.457 回答
0

当一个对话框出现时,它会在底层视图中添加一个图层来阻止其上的所有事件。为了实现您正在寻找的内容,您可以创建一个类似于对话框视图的视图并显示它而不是对话框。对于对话框和透明背景活动,您的目标将无法实现。

于 2013-08-29T05:54:18.900 回答
0

@banking 的方法又甜又短,我对它进行了一些修改,以便与布局文件一起使用,而不是使用 Java 代码创建它:

progress_spinner.xml:(在布局文件夹中)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center">

    <ProgressBar
        android:id="@+id/ProgressBar"
        style="?android:attr/progressBarStyleInverse"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:indeterminate="true" />

</RelativeLayout>

爪哇

ProgressBar progressBar=null;


public void addProgressBar(Activity activity) {
    ViewGroup rootFrameLayout = (ViewGroup) activity.getWindow().peekDecorView();
    LayoutInflater inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View progressModal = inflater.inflate(R.layout.progress_spinner, rootFrameLayout, false);
    rootFrameLayout.addView(progressModal, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));
    rootFrameLayout.invalidate();
    progressBar = (ProgressBar) progressModal.findViewById(R.id.ProgressBar);
}

public void hideProgressBar() {
    if (progressBar != null)
        progressBar.setVisibility(View.INVISIBLE);
}

public void showProgressBar() {
    if (progressBar != null)
        progressBar.setVisibility(View.VISIBLE);
}
于 2016-03-02T15:29:24.633 回答