编程创建和显示进度条

19

我在我的activity中以编程方式创建了一个ProgressBar,如下所示。我该如何使它显示出来?

progressBar = new ProgressBar(activity, null, android.R.attr.progressBarStyleSmall);

谢谢回复。但我没有使用ProgressDialog。我想让进度条在活动中显示为不确定状态。谢谢。 - user6304758
8个回答

13

您可以尝试使用以下代码将进度条以编程方式添加到布局中。

RelativeLayout layout = new RelativeLayout(this);
ProgressBar progressBar = new ProgressBar(YourActivity.this,null,android.R.attr.progressBarStyleLarge);
progressBar.setIndeterminate(true);
progressBar.setVisibility(View.VISIBLE);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100,100);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
layout.addView(progressBar,params);

setContentView(layout);

如何将进度条从视图中分离? - Sazzad Hissain Khan
@SazzadHissainKhan 你需要使用 setTag() 或 setId() 给它设置一个引用,然后在之后使用它来移除视图或将其可见性设置为 gone。 - Noya

1
ProgressBar prog = new ProgressBar(MainActivity.this);
linear1.addView(prog);

linear1是一个LinearLayout,MainActivity是你的活动。 根据你的需求更改linear1和MainActivity。


0

每个活动都有一个内容视图。这是您通过调用setContentView()设置的活动的根视图。屏幕上的每个视图都必须是此视图的子级(或子级的子级等)。唯一的例外是对话框,它们在单独的窗口中弹出,但这是另一个讨论。

如果您希望视图出现在屏幕上,则必须将其添加到内容视图内的某个ViewGroup中。

实际上,您通常使用进度条进行加载的方式不同。通常,您会将其添加到xml中,但将其可见性设置为GONE,以便它不会出现。然后,当您想要它出现时,将其设置为VISIBLE。因此,看起来进度条出现了,但实际上它一直隐藏在那里。


0

你可以像这样在xml文件中添加progressBar

<ProgressBar
            android:id="@+id/pbProgress"
            android:style="@style/Spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </ProgressBar>

可通过编程方式使其可见或不可见。

这是在xml文件中添加进度条的方法, 但如果您想以编程方式添加,可以使用progressBar.setVisibility(View.Visible)来显示进度条,或者使用progressBar.setVisibility(View.Gone)在活动中隐藏进度条。


3
我知道这是一个旧问题,但他确实在程序化地询问,你的回答讨论了如何通过XML手动添加,甚至没有包括如何使其在程序中可见或不可见。请编辑你的回答。 - MosabJ
如果你在 RecyclerView 中使用视图,那么这不是一个理想的解决方案。 - Pedro Paulo Amorim

-1

你可以在布局中使用:

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


    <com.airbnb.lottie.LottieAnimationView
        android:id="@+id/animation_view"
        android:layout_width="@dimen/toolbar_height_70dp"
        android:layout_height="@dimen/toolbar_height_70dp"
        android:layout_centerHorizontal="true"
        android:layout_centerInParent="true"
        android:layout_marginTop="10dp"
        app:lottie_autoPlay="true"
        app:lottie_fileName="lottie/circle-l.json"
        app:lottie_loop="true"

        />


</RelativeLayout>

在Java类中:

rlLoading.setVisibility(View.VISIBLE); Utility.disableEnableControls(false,rlRoot);

 public static void disableEnableControls(boolean enable, ViewGroup vg) {
    for (int i = 0; i < vg.getChildCount(); i++) {
        View child = vg.getChildAt(i);
        child.setEnabled(enable);
        if (child instanceof ViewGroup) {
            disableEnableControls(enable, (ViewGroup) child);
        }
    }
}

问题是关于仅通过编程使用 ProgressBar - MMK

-1

progressBar 是一个小部件(视图)。您需要将其添加到视图组中。

您可以使用 ProgressDialog 显示进度加载 示例:

ProgressDialog proDialog = ProgressDialog.show(this, "title", "message");

10
API级别26的ProgressDialog已被弃用。 - Asha
如何在程序中编程设置进度条消息 - Learning Always
你需要使用进度条而不是进度对话框。 - Cristian Cardoso

-1

我的方法是将它包装在AlertDialog中,调用对话框并在需要时使用show()、hide()和一些相关函数。这样,您就不需要在主XML布局文件中设置ProgressBar,并且可以在不通过编程方式设置布局参数的情况下调用它。

在布局文件layout_pb.xml中。

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center">
    <ProgressBar
        android:id="@+id/mProgress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </ProgressBar>
    <!--add other customize layout components you want-->
</LinearLayout>

在Java代码中

AlertDialog progressDialog = new AlertDialog.Builder(this)
                                 .setView(R.layout.layout_pb)
                                 .setPositiveButton("CANCEL", cancel listener ...)
                                 ...
                                 .show();

-3

首先需要导入包

import android.app.ProgressDialog;

然后使用这个:

    ProgressDialog progressDialog;

    progressDialog = new ProgressDialog(this);

    progressDialog.setMessage("MESSAGE");
    progressDialog.show();
    ........
    //tour task
    ........
    progressDialog.dismiss(); //dismiss progress bar

关于进度条的问题,问到的是进度对话框而不是进度条。 - saigopi.me

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