标签:android
ProgressBar是一种很常用的Ui,用于给复杂的操作显示进度,提供更好的用户相应。使用setProgress()incrementProgressBy()来设置进度和显示进度的增加或减少,正数表示增加,负数表示减少。
<LinearLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@android:style/Widget.ProgressBar.Small"/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@android:style/Widget.ProgressBar.Large"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下面是水平的进度条"/>
<ProgressBar
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/bar"
android:max="100"
style="@android:style/Widget.ProgressBar.Horizontal"/>
<ProgressBar
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/bar2"
android:max="100"
android:progressDrawable="@drawable/bar"
style="@android:style/Widget.ProgressBar.Horizontal"/>
</LinearLayout>
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@android:id/background"
android:drawable="#f00"
/>
<item
android:id="@android:id/progress"
android:drawable="@drawable/ic_launcher"/>
</layer-list>
package peng.liu.testview;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ProgressBar;
public class MainActivity extends Activity {
private int[] data = new int[100];
int hasData = 0;
int status = 0;
ProgressBar bar,bar2;
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
if(msg.what == 0x123){
bar.setProgress(status);
bar2.setProgress(status);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bar = (ProgressBar) findViewById(R.id.bar);
bar2 = (ProgressBar) findViewById(R.id.bar2);
new Thread(){
@Override
public void run() {
if(status < 100){
status = dowork();
handler.sendEmptyMessage(0x123);
}
}
};
}
public int dowork(){
data[hasData++] = (int) (Math.random()*100);
try{
Thread.sleep(100);
}
catch (Exception e){
e.printStackTrace();
}
return hasData;
}
}
Android的ProgressBar进度条-android学习之旅(三十一)
标签:android
原文地址:http://blog.csdn.net/lpjishu/article/details/46334985