今天復習一下以前的知識,補充一下ProgressBar控件
progressBar是進度條組件,通常用於用戶展示某個耗時操作完成的進度,而不讓用戶感覺是程序失去了響應,從而更好地提升用戶界面的友好性。
1)制定ProgressBar顯示風格
2)ProgressBar的分類
3)標題上ProgressBar的設置
4)ProgressBar的關鍵屬性
5)ProgressBar的關鍵方法
6)ProgressDialog的基礎使用
7)自定義ProgressBar樣式
1)制定ProgressBar顯示風格
style = "?android:attr/progressBarStyleLarge" 大環形進度條
style = "?android:attr/progressBarStyleSmall" 小環形進度條
style = "?android:attr/progressBarStyleHorizontal" 水平進度條
2)ProgressBar的分類
1.可以精確顯示進度(可以顯示刻度或者百分比)
2.不可以精確顯示精度(一直轉啊轉,類似於一個過場動畫)
3)標題上ProgressBar的設置
4)ProgressBar的關鍵屬性
android:max = "100" ——最大顯示進度
android:progress = "50" ——第一顯示進度
android:secondaryProgress = "80" ——第二顯示進度
android:indeterminate = "true" ——設置是否精確顯示
(true表示不精確顯示進度,false表示精確顯示進度)
5)ProgressBar的關鍵方法
1)setProgress(int) 設置第一進度
2)setSecondaryProgress(int) 設置第二進度
3)getProgress() 獲取第一進度
4)getSecondaryProgress() 獲取第二進度
5)incrementProgressBy(int) 增加或減少第一進度
6)incrementSecondaryProgressBy(int) 增加或減少第二進度
7)getMax() 獲取最大進度
案例:

1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 tools:context="com.example.progressbar.MainActivity" > 7 8 <ProgressBar 9 android:id="@+id/progressBar1" 10 style="?android:attr/progressBarStyleLarge" 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" /> 13 14 <ProgressBar 15 android:id="@+id/progressBar2" 16 android:layout_width="wrap_content" 17 android:layout_height="wrap_content"/><!-- 沒有設置默認中環形進度條 --> 18 19 <ProgressBar 20 android:id="@+id/progressBar3" 21 style = "?android:attr/progressBarStyleSmall" 22 android:layout_width="wrap_content" 23 android:layout_height="wrap_content"/> 24 25 <ProgressBar 26 android:secondaryProgress="80" 27 android:progress="50" 28 android:max="100" 29 android:id="@+id/horiz" 30 style="?android:attr/progressBarStyleHorizontal" 31 android:layout_width="match_parent" 32 android:layout_height="wrap_content"/> 33 34 <Button 35 android:id="@+id/add" 36 android:layout_width="wrap_content" 37 android:layout_height="wrap_content" 38 android:text="增加" /> 39 40 <Button 41 android:id="@+id/reduce" 42 android:layout_width="wrap_content" 43 android:layout_height="wrap_content" 44 android:text="減少" /> 45 46 <Button 47 android:id="@+id/reset" 48 android:layout_width="wrap_content" 49 android:layout_height="wrap_content" 50 android:text="重置" /> 51 52 <TextView 53 android:id="@+id/text" 54 android:layout_width="wrap_content" 55 android:layout_height="wrap_content"/> 56 57 </LinearLayout>

1 package com.example.progressbar; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.View; 6 import android.view.View.OnClickListener; 7 import android.view.Window; 8 import android.widget.Button; 9 import android.widget.ProgressBar; 10 import android.widget.TextView; 11 12 13 public class MainActivity extends Activity implements OnClickListener { 14 15 private ProgressBar progress ; 16 private Button add; 17 private Button reduce; 18 private Button reset; 19 private TextView text; 20 @Override 21 protected void onCreate(Bundle savedInstanceState) { 22 super.onCreate(savedInstanceState); 23 //啟用窗口特征,啟用帶進度和不帶進度的進度條 24 requestWindowFeature(Window.FEATURE_PROGRESS); 25 requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); 26 setContentView(R.layout.activity_main); 27 //顯示兩種進度條 28 setProgressBarVisibility(true);//水平進度條 29 setProgressBarIndeterminateVisibility(true);//環形進度條 30 //Max = 10000 31 setProgress(9999); 32 init(); 33 34 } 35 36 37 private void init() { 38 progress = (ProgressBar) findViewById(R.id.horiz); 39 add = (Button) findViewById(R.id.add); 40 reduce = (Button) findViewById(R.id.reduce); 41 reset = (Button) findViewById(R.id.reset); 42 text = (TextView) findViewById(R.id.text); 43 //getPeogress()獲取第一進度 44 int first = progress.getProgress(); 45 //獲取第二進度 46 int second = progress.getSecondaryProgress(); 47 //獲取最大進度 48 int max = progress.getMax(); 49 50 text.setText("第一進度百分比:"+(int)(first/(float)max*100)+"%,第一進度百分比:"+(int)(second/(float)max*100)+"%"); 51 add.setOnClickListener(this); 52 reduce.setOnClickListener(this); 53 reset.setOnClickListener(this); 54 } 55 56 @Override 57 public void onClick(View v) { 58 switch(v.getId()){ 59 case R.id.add :{ 60 //增加第一進度第二進度10個刻度 61 progress.incrementProgressBy(10); 62 progress.incrementSecondaryProgressBy(10); 63 64 break ; 65 } 66 case R.id.reduce :{ 67 progress.incrementProgressBy(-10); 68 progress.incrementSecondaryProgressBy(-10); 69 70 break ; 71 } 72 case R.id.reset :{ 73 progress.setProgress(50); 74 progress.setSecondaryProgress(80); 75 76 break ; 77 } 78 } 79 text.setText("第一進度百分比:"+(int)(progress.getProgress()/(float)progress.getMax()*100)+"%,第一進度百分比:"+(int)(progress.getSecondaryProgress()/(float)progress.getMax() * 100)+"%"); 80 } 81 }
6)ProgressDialog的基礎使用

1 private ProgressDialog prodialog ; 2 private Button show; 3 4 show = (Button) findViewById(R.id.show); 5 show.setOnClickListener(this); 6 7 8 case R.id.show :{ 9 /** 10 * 頁面顯示風格 11 */ 12 //新建ProgressDialog對象 13 prodialog = new ProgressDialog(MainActivity.this); 14 //設置顯示風格 15 prodialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 16 //設置標題 17 prodialog.setTitle("南方IT學院"); 18 //設置對話框文字信息 19 prodialog.setMessage("歡迎大家"); 20 //設置圖標 21 prodialog.setIcon(R.drawable.ic_launcher); 22 /** 23 * 設置關於ProgressBar屬性 24 */ 25 //設置最大進度 26 prodialog.setMax(100); 27 //設定初始化已經增長到的進度 28 prodialog.incrementProgressBy(50); 29 //進度條是明顯顯示進度的 30 prodialog.setIndeterminate(false); 31 32 /** 33 * 設定一個確定按鈕 34 */ 35 prodialog.setButton(DialogInterface.BUTTON_POSITIVE, "確定", new DialogInterface.OnClickListener() { 36 37 @Override 38 public void onClick(DialogInterface dialog, int which) { 39 Toast.makeText(MainActivity.this, "歡迎", Toast.LENGTH_LONG).show(); 40 } 41 }); 42 //是否可以通過返回按鈕退出對話框 43 prodialog.setCancelable(true); 44 45 //顯示ProgressDialog 46 prodialog.show(); 47 break ; 48 }
7)自定義ProgressBar樣式
style="@android:style/Widget.ProgressBar.Horizontal"
android:progressDrawable="@drawable/progress_bar"

1 <?xml version="1.0" encoding="utf-8"?> 2 <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 3 4 <item android:id="@android:id/background"> 5 <shape> 6 <corners android:radius="5dip" /> 7 8 <solid android:color="#88000000"/> 9 </shape> 10 </item> 11 <item android:id="@android:id/secondaryProgress"> 12 <clip> 13 <shape> 14 <corners android:radius="5dip" /> 15 16 <gradient 17 android:angle="270" 18 android:centerColor="#C6B7FF" 19 android:centerY="0.75" 20 android:endColor="#C3B2FF" 21 android:startColor="#B9A4FF" /> 22 </shape> 23 </clip> 24 </item> 25 <item android:id="@android:id/progress"> 26 <clip> 27 <shape> 28 <corners android:radius="5dip" /> 29 30 <gradient 31 android:angle="270" 32 android:centerColor="#74EBFF" 33 android:centerY="0.75" 34 android:endColor="#8EEFFF" 35 android:startColor="#57E8FF" /> 36 </shape> 37 </clip> 38 </item> 39 40 </layer-list>