ProgressBar本身進度條組件,它派生了:SeekBar和RatingBar兩個組件,他們的繼承關系如下:

**1、ProgressBar有兩個進度,**一個是android:progress,另一個是android:secondaryProgress。后者主要是為緩存需要所涉及的,比如在看網絡視頻時候都會有一個緩存的進度條以及還要一個播放的進度,在這里緩存的進度就可以是android:secondaryProgress,而播放進度就是android:progress,有了secondProgress,可以很方便定制ProgressBar。
**2、ProgressBar分為確定的和不確定的**,確定的是我們能明確看到進度,相反不確定的就是不清楚、不確定一個操作需要多長時間來完成,這個時候就需要用的不確定的ProgressBar了。屬性android:indeterminate如果設置為true的話,那么ProgressBar就可能是圓形的滾動條或者水平的滾動條(由樣式決定),但是我們一般時候,是直接使用Style類型來區分圓形還是水平ProgressBar的。
進度條的常用xml屬性如下:

其中根據**Style屬性**的設置不同,可以指定ProgressBar的3種環形進度條和1中水平進度條:
1,大環形進度條 ?style="?android:attr/progressBarStyleLarge或者style="?android:attr/progressBarStyleLargeInverse"
2,普通環形進度條:style="android:progressBarStyle"
3,小環形進度條:style="?android:attr/progressBarStyleSmall"
4,普通水平進度條:style=“?android:attr/progressBarStyleHorizontal”
**常用屬性解釋:**
android:max:設置進度條的最大值
android:progress:設置已完成進度條的進度值
android:progressDrawable:設置進度條軌道對應的Drawable對象。可以是一個LayerDrawable對象
android:indeterminate:設置屬性是true,設置進度條不精確顯示的進度,就是環形進度條的進度值
android:indeterminateDrawable:設置繪制不精確顯示的精度的Drawable對象,就是環形進度的軌道Drawable對象;
android:indeterminateDuration;設置不精確顯示精度的持續時間
mybar.xml
~~~
<?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>
<!-- 設置軌道上已完成背景的樣式 ,在上面-->
<item android:id="@android:id/progress"
android:drawable="@drawable/ok"></item>
</layer-list>
~~~
main.xml
~~~
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<!-- 大環形進度條 style="?android:attr/progressBarStyleLarge"-->
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLargeInverse"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!-- 普通的中等進度條 -->
<ProgressBar
android:id="@+id/progressBar2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!-- 小環形進度條 style="?android:attr/progressBarStyleSmall"-->
<ProgressBar
android:id="@+id/ProgressBar3"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="任務完成的進度"
android:textSize="20dp" />
<!-- 普通的水平進度條 -->
<ProgressBar
android:id="@+id/progressBar4"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="0"
android:visibility="visible" />
<!-- 水平進度條,改變軌道外觀 ,外觀圖片是在drawable里面,xml文件的layer-list元素編寫-->
<ProgressBar
android:id="@+id/progressBar5"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="100"
android:paddingTop="20dp"
android:progressDrawable="@drawable/mybar" />
</LinearLayout>
~~~
MainActivity.java
~~~
package com.hust.progresbartest;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ProgressBar;
public class MainActivity extends Activity {
/*給程序模擬填充長度為100的數組*/
private int[] data=new int[100];
int hasData=0;
//記錄ProgressBar的完成進度
int status=0;
ProgressBar bar,bar2;
//創建一個負責更新進度的Handler
Handler h=new Handler(){
public void handleMessage(Message msg){
if(msg.what==0x111){
//進度條設置已完成的值
bar.setProgress(status);
bar2.setProgress(status);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bar=(ProgressBar) findViewById(R.id.progressBar4);
bar2=(ProgressBar) findViewById(R.id.progressBar5);
//啟動線程來執行任務
new Thread(){
public void run(){
while(status<100){
//獲取耗時操作完成百分比
status=dowork();
//發送消息
h.sendEmptyMessage(0x111);
}
}
}.start();
}
//模擬一個耗時的操作
public int dowork(){
//為數組元素賦值
data[hasData++]=(int)(Math.random()*100);
try{
Thread.sleep(100);
}catch(InterruptedException e){
e.printStackTrace();
}
return hasData;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
~~~

- 前言
- Eclipse搭建android環境及Genymotion模擬器安裝問題解決方法
- 表格布局(TableLayout)及重要屬性
- 幀布局(FrameLayout)及屬性
- layout_width和width,layout_height和height
- UI組件之TextView及其子類
- UI組件之TextView及其子類(一)TextView和EditText
- UI組件之TextView及其子類(二)RadioButton和CheckBox
- UI組件之TextView及其子類(三)ToggleButton和Switch
- UI組件之TextView及其子類(四)AnalogClock,DigitalClock
- UI組件之TextView及其子類(五)計時器Chronometer
- UI組件之ImageView及其子類(一)ImageView顯示圖片
- UI組件之ImageView及其子類(二)ImageButton ,ZoomButton
- UI組件之AdapterView及其子類關系,Adapter接口及其實現類關系
- UI組件之AdapterView及其子類(一)三種Adapter適配器填充ListView
- UI組件之AdapterView及其子類(二)GridView網格視圖的使用
- UI組件之AdapterView及其子類(三)Spinner控件詳解
- UI組件之AdapterView及其子類(四)Gallery畫廊控件使用
- UI組件之AdapterView及其子類(五)ListView組件和ListActivity
- UI組件之AdapterView及其子類(六)ExpandableListView組件和ExpandableListActivity的使用
- UI組件之 ProgressBar及其子類(一)ProgressBar進度條的使用
- UI組件之ProgressBar及其子類(二)SeekBar拖動條和RatingBar星級評分條的使用
- ViewFlipper的功能和用法
- Toast的功能和用法
- TabHost選項卡的 功能和用法
- AlertDialog創建6種對話框的用法
- Android基于監聽的事件處理機制
- Android基于回調的事件處理
- Handler消息傳遞機制(一)
- Handler消息傳遞機制(二)Handler,Loop,Message,MessageQueue的工作原理
- 啟動Activity的兩種方式startActivity和startActivityForResult(一)
- 啟動Activity的兩種方式startActivity和startActivityForResult(二)
- Activity的生命周期理解
- Bundle在Activity之間交換數據
- 通過 Intent 傳遞類對象
- Intent對象詳解(一)
- Intent對象詳解(二)
- 使用指定的Action,Category調用系統Activity
- 使用Action,Data屬性啟動系統Activity
- Android數據存儲的三種方式-SharedPrefrences,File,SQLite