ProgressBar:進度條 還包括下面三種
ProgressBar有兩種展示方式,表盤形式(普通、小、大)和條形填充形式。在layout定義時,需要通過設施style屬性類設置展示方式。
XML重要屬性
android:max 設置進度條長度最大值
android:progress 設定度條當前進度值
android:secondargProgress 第二進度條進度值
android:progressBarStyle 默認進度條樣式
android:progressBarStyleHorizontal 水平樣式
style="@android:style/Widget.ProgressBar.Small" //小型圓形進度條
style="@android:style/Widget.ProgressBar.Small.Inverse" //小型圓形進度條
style="@android:style/Widget.ProgressBar.Inverse" //中型圓形進度條
style="@android:style/Widget.ProgressBar.Large" //大型圓形進度條
style="@android:style/Widget.ProgressBar.Large.Inverse" //大型圓形進度條
style="@android:style/Widget.ProgressBar.Horizontal" //水平進度條
其中,帶有Inverse參數和不帶有Inverse參數的style屬性區別在於:當進度條控件所在的界面背景顏色為白色時,需要使用帶有Inverse參數的style屬性,否則進度條將看不見。
重要方法
getMax():返回這個進度條的范圍的上限
getProgress():返回當前進度值
getSecondaryProgress() 返回次要當前進度值
incrementProgressBy(int diff) 指定增加的進度 即步長
islndeterminate():指示進度條是否在不確定模式下
setlndeterminate(boolean indeterminate):設置不確定模式下
setVisibility(int v):設置該進度條是否可視
1.ProgressDialog:對話框進度條
ProgressDialog pd=new ProgressDialog(this);
pd.setTitle("對話框標題");
pd.setMessage("信息,在下載....");
pd.setMax(100);
pd.setProgress(50);//進度
pd.setCancelable(false);//對話框按返回鍵不能撤銷(一直轉)
pd.setSecondaryProgress(80);//第二進度
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);樣式設置
pd.show();//最后要顯示
2.標題進度條
//設置進度條標題(該方法必須在setContentVi ew方法之前)
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS)
//顯示標題欄進度條(在setCon方法下) setProgressBarVisibility(true);
3.自定義進度條
通過一個圖片來自定義進度條:
android:indeterminateDrawable="@drawable/tupian_bg"
步驟:
1在res/drawable/下創建一個layer-list
2,設置ProgressBar的android:indeterminateDrawable屬性
<layer-list 資源文件的設置xmlns:android="http://schemas.android.com/apk/res/android" >
<item > 旋轉
<rotate android:drawable="@drawable/ic_launcher"
android:fromDegrees="0" 角度0
android:toDegrees="360" 到角度360旋轉
android:pivotX="50%" 軸中心
android:pivotY="50%"/>
</item>
</layer-list>
例子(先看下面的效果吧):
<ProgressBar android:id="@+id/pb_progressbar" style="@style/StyleProgressBarMini" android:layout_width="0dp" android:layout_weight="1" android:layout_height="40dp" android:background="@drawable/shape_progressbar_bg" android:max="100" android:progress="50" android:indeterminate="false" />
進度條的背景設置:drawable/shape_progressbar_bg.xml
<?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <!-- 邊框填充的顏色 --> <solid android:color="#ffcccc" /> <!-- 設置進度條的四個角為弧形 弧形的半徑--> <corners android:radius="90dp" /> <!--padding:邊界的間隔--> <padding android:bottom="2dp" android:left="2dp" android:right="2dp" android:top="2dp" /> </shape>
進度條的樣式設置,values/styles.xml/StyleProgressBarMini
<style name="StyleProgressBarMini" parent="@android:style/Widget.ProgressBar.Horizontal"> <item name="android:maxHeight">100dip</item> <item name="android:minHeight">10dip</item> <item name="android:indeterminateOnly">false</item> <item name="android:indeterminateDrawable">@android:drawable/progress_indeterminate_horizontal</item> <item name="android:progressDrawable">@drawable/shape_progressbar_mini</item> </style>
進度條顏色設置:drawable/shape_progressbar_mini.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"> <shape> <corners android:radius="90dp" /> <gradient android:angle="270" android:centerY="0.75" android:endColor="#98f7e9" android:startColor="#98f7e9" /> </shape> </item> <!-- 第二進度條的背景 左邊是圓形 但右邊不能--> <item android:id="@android:id/secondaryProgress"> <clip> <shape> <corners android:radius="5dip" /> <gradient android:angle="270" android:centerY="0.75" android:endColor="#df0024" android:startColor="#df0024" /> </shape> </clip> </item> <!--左邊是圓形 但右邊不能--> <item android:id="@android:id/progress"> <clip> <shape> <corners android:radius="90dp" /> <gradient android:angle="270" android:centerY="0.75" android:endColor="@color/title_background" android:startColor="@color/title_background" /> </shape> </clip> </item> </layer-list>
效果圖:(可見 進度條右邊不是圓弧 而是直角的)

解決辦法:
<!--左邊是圓形 但右邊不能--> <item android:id="@android:id/progress"> <clip> <shape> <corners android:radius="90dp" /> <gradient android:angle="270" android:centerY="0.75" android:endColor="@color/title_background" android:startColor="@color/title_background" /> </shape> </clip> </item> <!--修改為: 進度條的背景 --> <!-- 進度條 --> <item android:id="@android:id/progress"> <scale android:scaleWidth="100%" android:drawable="@drawable/shape_progress_bg" /> </item>
drawable/shape_progress_bg.xml 當然android:drawable 也可以使用.9圖片
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="@color/title_background" /> <corners android:radius="90dp" /> </shape>
效果如下:

