如何以编程方式创建ProgressBar?

时间:2022-03-01 23:01:22

My application need to create a small ProgressBar programmatically. ProgressBar doesn't have method to set the style (I want a small progressBar). The constructor can take a AttributeSet, however it is a interface and require me implements a set of functions. Is there a way to set the ProgressBar small style? (I can't use xml to create ProgressBar)

我的应用程序需要以编程方式创建一个小型ProgressBar。 ProgressBar没有设置样式的方法(我想要一个小的progressBar)。构造函数可以使用AttributeSet,但它是一个接口,需要我实现一组函数。有没有办法设置ProgressBar小样式? (我不能使用xml来创建ProgressBar)

4 个解决方案

#1


162  

Most of the time if you provide an AttributeSet manually you have to use one of Android's. Luckily, they've exposed the attribute set that describes a small progress bar. Use this code:

大多数情况下,如果您手动提供AttributeSet,则必须使用Android的一个。幸运的是,他们暴露了描述一个小进度条的属性集。使用此代码:

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

#2


9  

Create a layout xml file in res/layout directory with desired progress bar containig all attributes you need:

在res / layout目录中创建一个布局xml文件,所需的进度条包含您需要的所有属性:

<?xml version="1.0" encoding="utf-8"?>
<ProgressBar
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" ... />

Next in the Activity class you can create ProgressBar object from that layout:

接下来,在Activity类中,您可以从该布局创建ProgressBar对象:

LayoutInflater inflater = getLayoutInflater();
    ProgressBar bar = (ProgressBar ) inflater.inflate(R.layout.small_progress_bar, null);

where R.layout.small_progress_bar links to your layout xml file.

其中R.layout.small_progress_bar链接到您的布局xml文件。

Can you still not use xml file?

你还能不用xml文件吗?

#3


1  

Activity.java

Activity.java

  progressBar = (ProgressBar) findViewById(R.id.progressbar);
 `progressBar.setVisibility(View.VISIBLE);`// To Show ProgressBar 
 `progressBar.setVisibility(View.INVISIBLE);` //To Hide ProgressBar

如何以编程方式创建ProgressBar?

Check here ProgressDialog is deprecated.What is the alternate one to use?

在这里检查ProgressDialog已被弃用。可以使用的替代方法是什么?

#4


-3  

Full code for adding progress bar programmatically

以编程方式添加进度条的完整代码

private ProgressDialog mProgressDialog;
    mProgressDialog = new ProgressDialog(this);
    mProgressDialog.setMessage("Signing........");
    mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    mProgressDialog.setCancelable(false);

Call below method to show bar

调用以下方法显示栏

mProgressDialog.show();

call below to hide the bar

请拨打以下隐藏栏

mProgressDialog.dismiss();

#1


162  

Most of the time if you provide an AttributeSet manually you have to use one of Android's. Luckily, they've exposed the attribute set that describes a small progress bar. Use this code:

大多数情况下,如果您手动提供AttributeSet,则必须使用Android的一个。幸运的是,他们暴露了描述一个小进度条的属性集。使用此代码:

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

#2


9  

Create a layout xml file in res/layout directory with desired progress bar containig all attributes you need:

在res / layout目录中创建一个布局xml文件,所需的进度条包含您需要的所有属性:

<?xml version="1.0" encoding="utf-8"?>
<ProgressBar
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" ... />

Next in the Activity class you can create ProgressBar object from that layout:

接下来,在Activity类中,您可以从该布局创建ProgressBar对象:

LayoutInflater inflater = getLayoutInflater();
    ProgressBar bar = (ProgressBar ) inflater.inflate(R.layout.small_progress_bar, null);

where R.layout.small_progress_bar links to your layout xml file.

其中R.layout.small_progress_bar链接到您的布局xml文件。

Can you still not use xml file?

你还能不用xml文件吗?

#3


1  

Activity.java

Activity.java

  progressBar = (ProgressBar) findViewById(R.id.progressbar);
 `progressBar.setVisibility(View.VISIBLE);`// To Show ProgressBar 
 `progressBar.setVisibility(View.INVISIBLE);` //To Hide ProgressBar

如何以编程方式创建ProgressBar?

Check here ProgressDialog is deprecated.What is the alternate one to use?

在这里检查ProgressDialog已被弃用。可以使用的替代方法是什么?

#4


-3  

Full code for adding progress bar programmatically

以编程方式添加进度条的完整代码

private ProgressDialog mProgressDialog;
    mProgressDialog = new ProgressDialog(this);
    mProgressDialog.setMessage("Signing........");
    mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    mProgressDialog.setCancelable(false);

Call below method to show bar

调用以下方法显示栏

mProgressDialog.show();

call below to hide the bar

请拨打以下隐藏栏

mProgressDialog.dismiss();