指示当前进度
1.在布局中添加ProgressBar
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<ProgressBar
android:id="@+id/progress_bar"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
package com.example.progressbardemo;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ProgressBar;
public class MainActivity extends Activity {
String tag = MainActivity.class.getSimpleName();
private static int PROGRESS = 0;
private ProgressBar mProgress;
private int mProgressStatus = 0;
Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
mProgress.setProgress(mProgressStatus);
}
};
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
mProgress = (ProgressBar) findViewById(R.id.progress_bar);
new Thread(new Runnable() {
public void run() {
while (mProgressStatus < 100) {
mProgressStatus = doWork();
mHandler.sendEmptyMessage(0);
Log.d(tag, (System.currentTimeMillis()) / 1000 + ""
+ mProgressStatus);
}
}
}).start();
}
public int doWork() {
try {
PROGRESS += 10;
Thread.currentThread();
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return PROGRESS;
}
}
不确定进度显示
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<ProgressBar
android:id="@+id/progress_bar"
style="@android:style/Widget.ProgressBar.Large"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginRight="5dp" />
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/progress_bar"
android:layout_centerHorizontal="true"
android:paddingTop="8dp"
android:text="加载中..." />
</RelativeLayout>
只要简单的使用一个布局就可以完成这样动态的效果!
翻译地址
https://developer.android.com/reference/android/widget/ProgressBar.html