Android ProgressBar示例
时间:2020-02-23 14:29:09 来源:igfitidea点击:
欢迎使用Android ProgressBar示例。
今天,我们将在应用程序中实现androidProgressBar。
有两种类型的进度条:水平和圆形。
我们将在android应用程序中创建这两个进度列。
Android ProgressBar
Android ProgressBar是一个图形视图指示器,显示了一些进度。
Android进度列显示一个代表任务完成的列。
android中的进度条很有用,因为它使用户有时间完成任务。
使用ProgressBar是一种很好的用户体验做法,因为它向用户显示给定任务的进度状态(例如下载图像)。
Android ProgressBar属性
下面给出了一些用于描述ProgressBar的重要属性。
- android:max:我们可以使用此属性设置ProgressBar的最大值。
默认情况下,进度条最大值为100 - android:indeterminate:根据时间是否确定来设置布尔值。
将此属性设置为false将显示实际进度。
否则,如果设置为true,则会显示一个循环动画以表明进度正在发生 - android:minHeight:用于设置ProgressBar的高度
- android:minWidth:用于设置ProgressBar的宽度
- android:progress:用于设置进度条值递增的数量
style:默认情况下,进度条将显示为旋转轮。
如果我们希望将其显示为水平条,则需要将属性设置为:style ="?android:attr/progressBarStyleHorizontal"
在本教程中,我们将创建一个ProgressBar并通过在线程内部进行更新来增加其值。
递增值以缓慢显示进度后,我们将使线程休眠200毫秒。
Android进度条码
activity_main.xml包含一个RelativeLayout作为父视图,其中包含一个Horizontal ProgressBar和一个Circular以及一个TextView以数字形式显示进度。
activity_main.xml
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="23dp"
android:layout_marginTop="20dp"
android:indeterminate="false"
android:max="100"
android:minHeight="50dp"
android:minWidth="200dp"
android:progress="1"
<ProgressBar
android:id="@+id/progressBar_cyclic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="50dp"
android:minWidth="50dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/progressBar"
android:layout_below="@+id/progressBar"
</RelativeLayout>
在上述布局中,水平进度条值会按照android:progress中的设置更新一个。
除非活动停止,否则圆形进度条将连续运行。
package com.theitroad.progressbar;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private ProgressBar progressBar;
private int progressStatus = 0;
private TextView textView;
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
textView = (TextView) findViewById(R.id.textView);
//Start long running operation in a background thread
new Thread(new Runnable() {
public void run() {
while (progressStatus < 100) {
progressStatus += 1;
//Update the progress bar and display the
//current value in the text view
handler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressStatus);
textView.setText(progressStatus+"/"+progressBar.getMax());
}
});
try {
//Sleep for 200 milliseconds.
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
}
在上面的代码中,progressBar每200毫秒更新一次,进度条更新使用setProgress()设置。
处理程序用于运行后台线程。
线程将运行直到progressStatus值达到100。

