【问题标题】:Android: How to Create a Modal Progress "Wheel" Overlay?Android:如何创建模态进度“轮”叠加?
【发布时间】:2010-03-09 12:47:57
【问题描述】:

我想在我的视图上显示一个模态进度“轮”覆盖。

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 代码相同)。

【问题讨论】:

  • 应该重新选择正确的答案,似乎选择的答案只是指向教程的指针,每个人都赞成的那个有更好的例子。

标签: android progressdialog


【解决方案1】:

不确定是否有更好的方法,但您可以通过使用 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>

【讨论】:

  • 谢谢,我相信这也可以,所以我投了赞成票,但我选择了其他解决方案,因为它更易于实施。
  • 只针对这个问题的新手。 AbsoluteLayout 自 API 级别 3 起已弃用。这对你们意味着什么。
  • 由于 AbsoluteLayout 已被贬低,因此请使用 FrameLayout 将事物置于彼此之上,就像在 EZDsIt 的答案中一样。
【解决方案2】:

为了在深色背景上创建全屏进度,我使用 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>

【讨论】:

  • 我真的很喜欢这个想法,很容易实现。我只是建议将OnTouchEventListener 添加到返回 true 的 RelativeLayout,这样您的触摸事件就不会传递到底层的 FrameLayout。
  • 另一种解决方案是使 FrameLayout android:clickable="true"
【解决方案3】:

Klarth 的解决方案对我有用,谢谢!

在我的例子中,我使用 layout_gravity 来放置 progress_indicator 的中心,而不是使用显式坐标。

    <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"
        />

【讨论】:

    【解决方案4】:

    您查看过这个tutorial 吗?在页面的末尾,它讨论了如何创建自定义对话框,这可能会帮助您使用自己的背景放置一个微调进度对话框。

    【讨论】:

    • 对于那些想知道为什么这个答案很少有人赞成的人,可能是这样的:“注意:Android 包含另一个名为 ProgressDialog 的对话框类,它显示一个带有进度条的对话框。这个小部件已被弃用,因为它会阻止用户与显示进度时的应用程序。”您应该使用下一个答案并将其包含在您的布局中。
    【解决方案5】:

    您只需将 .setCanceledOnTouchOutside(false); 添加到您的 ProgressDialog。

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

    【讨论】:

    • 已弃用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-08
    相关资源
    最近更新 更多