在Android上显示进度对话框,而不会阻止其他UI元素的点击

时间:2013-08-29 05:41:49

标签: android

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

6 个答案:

答案 0 :(得分: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();
}

答案 1 :(得分:1)

您可以在transparent中使用ProgressBar创建完全center线性或任何其他类型的布局,并在包含所有其他控件的FrameLayout中添加此布局。现在,当您希望显示progress bar时,请layout visible生成invisible,并在不希望它显示时生成{{1}}。

答案 2 :(得分: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);
   }
}

答案 3 :(得分:1)

bankings method我认为这是最好和最简单的实施方式。只是为了方便任何想要使用它的人,而是让方法返回ProgressBar,当你想要删除这个进度条时,你可以使用这个返回值来设置它的可见性,如图所示

progressBar.setVisibility(View.GONE);

希望它能为上述答案增添价值。

答案 4 :(得分:0)

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

答案 5 :(得分:0)

@ banking&#39; s method很简单,我已经修改了一些用于布局文件而不是用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);
}