//注意: //在xml文件中 //style="@android:style/Widget.ProgressBar.Horizontal"采用系统自带的样式 1 main.xml文件如下 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ProgressBar android:id="@+id/progressBar" android:layout_width="wrap_content" android:layout_height="30dip" android:visibility="visible" style="@android:style/Widget.ProgressBar.Horizontal" /> </LinearLayout> 2 Activity如下 package cn.com; import android.app.Activity; import android.os.Bundle; import android.widget.ProgressBar; public class TestProgressBarActivity extends Activity { private ProgressBar progressBar = null; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); progressBar = (ProgressBar) findViewById(R.id.progressBar); // 从这到下是关键 Thread thread = new Thread(new Runnable() { @Override public void run() { int progressBarMax = progressBar.getMax();//得到progressBar的最大刻度 try { while (progressBarMax != progressBar.getProgress()) { int everyProgress = progressBarMax / 10;//progressBar的最大刻度细分为十份 int currentprogress = progressBar.getProgress();//得到当前刻度 progressBar.setProgress(currentprogress + everyProgress);//设置更新后的刻度 Thread.sleep(1000);//线程睡眠一秒 } } catch (InterruptedException e) { e.printStackTrace(); } } }); thread.start(); //开启线程.关键结束 } }
时间: 2024-10-14 19:25:37