Android:如何创建模态进度“轮”叠加?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2409063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:45:35  来源:igfitidea点击:

Android: How to Create a Modal Progress "Wheel" Overlay?

androidprogressdialog

提问by Edwin Lee

I would like to show a modal progress "wheel" overlay on my view.

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

The ProgressDialog comes close, but I do not want the dialog background or border.

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

I tried setting the background drawable of the dialog window:

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

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();

but to no avail (i.e. still looks the same as without the ...setBackgroundDrawable code).

但无济于事(即仍然看起来与没有 ...setBackgroundDrawable 代码相同)。

采纳答案by Steve Haley

Have you checked out this tutorial? At the end of the page it talks about how to create a custom dialog, and that might help you put a spinner progress dialog box with your own backgrounds.

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

回答by Klarth

Not sure if there is a better way, but you could get the spinner wheel on its own by using a ProgressBarand setting it to be interdeterminate. If you are using an AbsoluteLayoutyou can put it over other views. This layout should demonstrate this method with XML:

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

回答by EZDsIt

In order to create a full screen progress on a darkened background I use a FrameLayout and set the visibility of the RelativeLayout to VISIBLE when required or GONE when the long operation is done:

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

回答by paiego

Klarth's solution worked for me, thanks!

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

In my case I used the layout_gravity for center placement of the progress_indicator, rather than using explicit coordinates.

在我的例子中,我使用 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"
        />