【问题标题】:Create and show ProgressBar programmatically以编程方式创建和显示 ProgressBar
【发布时间】:2016-11-04 18:49:05
【问题描述】:

我在我的活动中以编程方式创建了一个ProgressBar,如下所示。如何让它显示?

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

【问题讨论】:

  • 感谢您的回复。但我没有使用 ProgressDialog。我希望 progressBar 在活动中显示为不确定的。谢谢。

标签: android android-layout android-activity android-progressbar


【解决方案1】:

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

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

【讨论】:

  • 如何从视图中分离进度条?
  • @SazzadHissainKhan 您需要通过使用 setTag() 或 setId() 来引用它,然后使用它来删除视图或将其可见性设置为之后消失
【解决方案2】:

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

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

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

【讨论】:

  • API 级别 26 ProgressDialog 已弃用。
  • 然后如何以编程方式设置消息进度条
  • 你需要使用进度条而不是进度对话框。
【解决方案3】:

你可以像这样在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) 来隐藏活动的进度条。

【讨论】:

  • 我知道这是一个老问题,但他真的问 Programmatically,你的回答讨论了如何通过 XML 手动添加它,你甚至没有包括如何制作它以编程方式可见和不可见。请编辑您的答案
  • 这不是您在 RecyclerView 中使用视图的理想解决方案。
【解决方案4】:

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

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

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

【讨论】:

    【解决方案5】:
    ProgressBar prog = new ProgressBar(MainActivity.this);
    linear1.addView(prog);
    

    linear1 是一个 LinearLayout,而 MainActivity 是您的活动。 根据您的需要更改 linear1 和 MainActivity。

    【讨论】:

      【解决方案6】:

      你可以在布局中使用:

      <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
      【解决方案7】:

      我的方法是将它包装在一个 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();
      

      【讨论】:

        【解决方案8】:

        首先你需要导入包

        import android.app.ProgressDialog;
        

        那就用这个吧,

            ProgressDialog progressDialog;
        
            progressDialog = new ProgressDialog(this);
        
            progressDialog.setMessage("MESSAGE");
            progressDialog.show();
            ........
            //tour task
            ........
            progressDialog.dismiss(); //dismiss progress bar
        

        【讨论】:

        • 关于进度条而不是进度对话框的问题
        猜你喜欢
        • 2011-04-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多