一、常用属性
android:progress=”0” —-设置第一层进度条的初始值
android:max=”100” —设置进度条的最大值
android:secondaryProgress=”10” –设置第二层进度条的初始值
二、常用方法
int getMax():返回这个进度条的最大值
int getProgress();返回进度条当前进度
int getSecondProgress():返回当前次要进度
void incrementProgressBy(int diff):指定增加的进度,每次推进的步伐
boolean isInderterminate():指示进度条是否在不确定模式下
void setIndeterminate(boolean indeterminate):设置不确定模式下,用于无法确定时间任务
void setVisibility(int v):设置该进度条是否可视。
三、代码实现
activity_main.XML文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" tools:context="com.example.g160628_06.MainActivity">
<ProgressBar
android:id="@+id/pb_bar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_text"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下载"
android:id="@+id/button"
android:onClick="down"
/>
</LinearLayout>
MainActivity.java文件
package com.example.g160628_06;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private ProgressBar pb_bar;
private TextView tv_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pb_bar = (ProgressBar) findViewById(R.id.pb_bar);
tv_text = (TextView) findViewById(R.id.tv_text);
}
public void down(View view){
new MyDown().start();
}
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int i = msg.what;
tv_text.setText(i+"");
}
};
class MyDown extends Thread{
@Override
public void run() {
super.run();
for (int i = 0; i <= 100 ; i++) {
pb_bar.setProgress(i);
handler.sendEmptyMessage(i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
实现效果