進度條(Progressbar)
提供如下一些樣式改變進度條的外觀
@android:style/Widget.ProgressBar.Horizontal(水平進度條)
@android:style/Widget.ProgressBar.Inverse(普通大小的環形進度條)
@android:style/Widget.ProgressBar.Large(大環形進度條)
@android:style/Widget.ProgressBar.Large.Inverse(大環形進度條)
@android:style/Widget.ProgressBar.Small(小環形進度條)
@android:style/Widget.ProgressBar.Small.Inverse(小環形進度條)
常用屬性:
max:設置該進度條的最大值
progress:設置該進度條已完成的進度值
progressDrawable:設置該進度條的軌道對應的Drawable對象(是一個xml文件)
下面我們直接看代碼:
1.Activity
//進度條 public class ProgressBarActivity extends Activity { private ProgressBar progressBarDefaultStyle1; private ProgressBar progressBarDefaultStyle2; private Button button; private Handler handler = new Handler(){ public void handleMessage(Message msg) { int flag = msg.arg1; if(flag <= 100){ progressBarDefaultStyle1.setProgress(flag); } if (flag <= 200) { progressBarDefaultStyle2.setProgress(flag/2); } if (flag==201){ progressBarDefaultStyle1.setProgress(0); progressBarDefaultStyle2.setProgress(0); button.setEnabled(true); timerTask.cancel(); } } }; private Timer timer = new Timer(); private SendMsgTimerTask timerTask = new SendMsgTimerTask(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.progress_bar); progressBarDefaultStyle1 = (ProgressBar)findViewById(R.id.progressBarDefaultStyle1Id); progressBarDefaultStyle2 = (ProgressBar)findViewById(R.id.progressBarDefaultStyle2Id); button = (Button) findViewById(R.id.buttonId); button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { button.setEnabled(false); timer.schedule(timerTask, 5, 100); } }); } class SendMsgTimerTask extends TimerTask{ int flag = 0; public void run() { Message msg = new Message(); msg.arg1 = ++flag; handler.sendMessage(msg); Log.i("msg.arg1", "發消息:"+msg.arg1); } } @Override protected void onDestroy() { super.onDestroy(); timer.cancel(); } }
2.xml布局文件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="5dp" > <!-- 定義一個普通大小的環形進度條 --> <ProgressBar android:id="@+id/progressBarInverseStyleId" style="@android:style/Widget.ProgressBar.Inverse" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <!-- 定義一個大環形進度條 --> <ProgressBar android:id="@+id/progressBarLargeStyleId" style="@android:style/Widget.ProgressBar.Large" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/progressBarInverseStyleId" /> <!-- 定義一個大環形進度條 --> <ProgressBar android:id="@+id/progressBarLargeInverseStyleId" style="@android:style/Widget.ProgressBar.Large.Inverse" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/progressBarLargeStyleId" /> <!-- 定義一個小環形進度條 --> <ProgressBar android:id="@+id/progressBarSmallStyleId" style="@android:style/Widget.ProgressBar.Small" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/progressBarLargeInverseStyleId" /> <!-- 定義一個小環形進度條 --> <ProgressBar android:id="@+id/progressBarSmallInverseStyleId" style="@android:style/Widget.ProgressBar.Small.Inverse" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/progressBarSmallStyleId" /> <!-- 定義一個默認樣式的水平進度條 --> <ProgressBar android:id="@+id/progressBarDefaultStyle1Id" style="@android:style/Widget.ProgressBar.Horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/progressBarSmallInverseStyleId" android:max="100" /> <!-- 定義一個指定樣式的水平進度條 --> <!-- bar_state是一個圖片狀態文件 --> <ProgressBar android:id="@+id/progressBarDefaultStyle2Id" style="@android:style/Widget.ProgressBar.Horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/progressBarDefaultStyle1Id" android:max="100" android:progressDrawable="@drawable/bar_state" /> <Button android:id="@+id/buttonId" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/progressBarDefaultStyle2Id" android:text="模擬耗時操作" /> </RelativeLayout>
3.進度條的布局文件
<?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="@drawable/no"/> <!-- 定義軌道的成功圖像 --> <item android:id="@android:id/progress" android:drawable="@drawable/yes"/> </layer-list>
4.效果顯示圖