Android: 如何创建一个模态进度“轮”覆盖层?

8

我希望在我的视图上显示一个模态进度“轮”叠加层。

ProgressDialog很接近,但我不想要对话框的背景或边框。

我尝试设置对话框窗口的背景可绘制项:

this.progressDialog = new ProgressDialog(Main.this);

this.progressDialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
this.progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
this.progressDialog.setCancelable(false);
this.progressDialog.setIndeterminate(true);

this.progressDialog.show();

但是没有效果(即仍然与没有设置setBackgroundDrawable代码时一样)。


也许应该重新选择正确的答案,因为被选中的只是指向教程的指针,而被所有人投票支持的那个有更好的示例。 - JPM
5个回答

13

不确定是否有更好的方法,但您可以通过使用ProgressBar并将其设置为不确定来单独获取旋转器轮。如果您正在使用AbsoluteLayout,则可以将其放置在其他视图上方。此布局应使用XML演示此方法:

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout android:id="@+id/AbsoluteLayout"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <ProgressBar android:id="@+id/ProgressBar"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:indeterminate="true" android:indeterminateOnly="true"
        android:isScrollContainer="true" android:layout_x="100dip"
        android:layout_y="10dip" android:soundEffectsEnabled="true"></ProgressBar>
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="@string/hello"
        android:layout_gravity="center_horizontal" android:gravity="center_horizontal"
        android:layout_y="25dip" />
</AbsoluteLayout>

1
谢谢,我相信这个方法也可以,所以我投了赞成票,但是我选择了另一个解决方案,因为它更容易实现。 - Edwin Lee
对于所有初次接触这个问题的人来说,AbsoluteLayout自API Level 3起已被弃用。无论对你们来说意味着什么。 - Hermann Klecker
由于AbsoluteLayout已经被弃用,建议使用FrameLayout来将元素叠放在一起,就像EZDsIt的回答中所示。 - lhermann

11
为了在暗色背景上创建全屏进度条,我使用 FrameLayout,并在需要时将 RelativeLayout 的可见性设置为 VISIBLE,当长时间操作完成时将其设置为 GONE:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ScrollView 
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

        <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:orientation="vertical"
        android:padding="3dip" >

            <!-- Your regular UI here -->

    </LinearLayout>
   </ScrollView>

   <RelativeLayout
       android:id="@+id/relativelayout_progress"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:visibility="gone"
       android:background="#aa000022" >

       <ProgressBar 
           android:layout_centerInParent="true"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:indeterminateOnly="true" />

    </RelativeLayout>
</FrameLayout>

4
我非常喜欢这个想法,实现起来非常容易。我建议在RelativeLayout上添加一个 OnTouchEventListener,并返回 true,这样您的触摸事件就不会传递到底层的FrameLayout。 - Gerrit-K
1
另一种解决方案是将FrameLayout设置为android:clickable="true" - bgs

3

Klarth的解决方案对我很有帮助,谢谢!

在我的情况下,我使用了layout_gravity来使进度指示器居中,而不是使用显式坐标。

    <ProgressBar android:id="@+id/pb"
        android:layout_width="40dp" 
        android:layout_height="40dp"
        android:indeterminate="true" 
        android:indeterminateOnly="true"
        android:isScrollContainer="true" 
        android:layout_gravity="center_vertical|center_horizontal"
        android:soundEffectsEnabled="false"
        />

1
你看过这个tutorial了吗?在页面的末尾,它讲述了如何创建自定义对话框,这可能会帮助你将一个带有自己背景的旋转进度对话框放置在其中。

对于那些想知道为什么这个答案没有很多赞的人,可能是因为这个原因:“注意:Android 包含另一个名为 ProgressDialog 的对话框类,它显示带有进度条的对话框。由于该小部件在显示进度时会阻止用户与应用程序进行交互,因此该小部件已被弃用。”您应该使用下一个答案并将其包含在您的布局中。 - findusl

0

你只需要在ProgressDialog中添加.setCanceledOnTouchOutside(false);即可。

 ProgressDialog dialog = new ProgressDialog(getApplicationContext());
 dialog.setMessage("your message...");
 dialog.setIndeterminate(true);
 dialog.setCanceledOnTouchOutside(false);
 dialog.show();

已弃用 - Fayaz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接