多式樣ProgressBar
普通圓形ProgressBar
該類型進度條也就是一個表示運轉的過程,例如發送短信,連接網絡等等,表示一個過程正在執行中。
一般只要在XML布局中定義就可以了。
- <progressBar android:id="@+id/widget43"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical">
- </ProgressBar>
此時,沒有設置它的風格,那么它就是圓形的,一直會旋轉的進度條。
各大小樣式圓形ProgressBar
超大號圓形ProgressBar
此時,給設置一個style風格屬性后,該ProgressBar就有了一個風格,這里大號ProgressBar的風格是:
- style="?android:attr/progressBarStyleLarge"
完整XML定義是:
- <progressBar android:id="@+id/widget196"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- style="?android:attr/progressBarStyleLarge">
- </ProgressBar>
小號圓形ProgressBar
小號ProgressBar對應的風格是:
- style="?android:attr/progressBarStyleSmall"
標題型圓形ProgressBar
標題型ProgressBar對應的風格是:
- style="?android:attr/progressBarStyleSmallTitle"
完整XML定義是:
- <progressBar android:id="@+id/widget110"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- style="?android:attr/progressBarStyleSmallTitle">
- </ProgressBar>
代碼中實現:
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
- //請求窗口特色風格,這里設置成不明確的進度風格
- setContentView(R.layout.second);
- setProgressBarIndeterminateVisibility(true);
- //設置標題欄中的不明確的進度條是否可以顯示
- }
長形進度條
布局中的長形進度條
①首先在XML進行布局
- <progressBar android:id="@+id/progressbar_updown"
- android:layout_width="200dp"
- android:layout_height="wrap_content"
- style="?android:attr/progressBarStyleHorizontal"
- android:layout_gravity="center_vertical"
- android:max="100"
- android:progress="50"
- android:secondaryProgress="70" >
講解:
②代碼中運用
- private ProgressBar myProgressBar;
- //定義ProgressBar
- myProgressBar = (ProgressBar) findViewById(R.id.progressbar_updown);
- //ProgressBar通過ID來從XML中獲取
- myProgressBar.incrementProgressBy(5);
- //ProgressBar進度值增加5
- myProgressBar.incrementProgressBy(-5);
- //ProgressBar進度值減少5
- myProgressBar.incrementSecondaryProgressBy(5);
- //ProgressBar背后的第二個進度條 進度值增加5
- myProgressBar.incrementSecondaryProgressBy(-5);
- //ProgressBar背后的第二個進度條 進度值減少5
頁面標題中的長形進度條
代碼實現:
①先設置一下窗口風格特性
- requestWindowFeature(Window.FEATURE_PROGRESS);
- //請求一個窗口進度條特性風格
- setContentView(R.layout.main);
- setProgressBarVisibility(true);
- //設置進度條可視
②然后設置進度值
- setProgress(myProgressBar.getProgress() * 100);
- //設置標題欄中前景的一個進度條進度值
- setSecondaryProgress(myProgressBar.getSecondaryProgress() * 100);
- //設置標題欄中后面的一個進度條進度值
- //ProgressBar.getSecondaryProgress() 是用來獲取其他進度條的進度值
ProgressDialog
ProgressDialog中的圓形進度條
ProgressDialog一般用來表示一個系統任務或是開啟任務時候的進度,有一種稍等的意思。
代碼實現:
- ProgressDialog mypDialog=new ProgressDialog(this);
- //實例化
- mypDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
- //設置進度條風格,風格為圓形,旋轉的
- mypDialog.setTitle("Google");
- //設置ProgressDialog 標題
- mypDialog.setMessage(getResources().getString(R.string.second));
- //設置ProgressDialog 提示信息
- mypDialog.setIcon(R.drawable.android);
- //設置ProgressDialog 標題圖標
- mypDialog.setButton("Google",this);
- //設置ProgressDialog 的一個Button
- mypDialog.setIndeterminate(false);
- //設置ProgressDialog 的進度條是否不明確
- mypDialog.setCancelable(true);
- //設置ProgressDialog 是否可以按退回按鍵取消
- mypDialog.show();
- //讓ProgressDialog顯示
ProgressDialog中的長形進度條
代碼實現:
- ProgressDialog mypDialog=new ProgressDialog(this);
- //實例化
- mypDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
- //設置進度條風格,風格為長形,有刻度的
- mypDialog.setTitle("地獄怒獸");
- //設置ProgressDialog 標題
- mypDialog.setMessage(getResources().getString(R.string.second));
- //設置ProgressDialog 提示信息
- mypDialog.setIcon(R.drawable.android);
- //設置ProgressDialog 標題圖標
- mypDialog.setProgress(59);
- //設置ProgressDialog 進度條進度
- mypDialog.setButton("地獄曙光",this);
- //設置ProgressDialog 的一個Button
- mypDialog.setIndeterminate(false);
- //設置ProgressDialog 的進度條是否不明確
- mypDialog.setCancelable(true);
- //設置ProgressDialog 是否可以按退回按鍵取消
- mypDialog.show();
- //讓ProgressDialog顯示
AlertDialog.Builder
AlertDialog中的圓形ProgressBar
①先來設計一個Layout,待會兒作為一個View,加入AlertDialog.Builder
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_gravity="center_horizontal"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <LinearLayout android:id="@+id/LinearLayout01"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- </LinearLayout>
- <ProgressBar android:layout_gravity="center_vertical|center_horizontal"
- android:layout_height="wrap_content"
- android:progress="57"
- android:id="@+id/myView_ProgressBar2"
- android:layout_width="wrap_content">
- </ProgressBar>
- </LinearLayout>
②代碼羅
- private AlertDialog.Builder AlterD,AlterD2;
- //定義提示對話框
- private LayoutInflater layoutInflater;
- //定義布局過濾器
- private LinearLayout myLayout;
- //定義布局
- layoutInflater2=(LayoutInflater) getSystemService(this.LAYOUT_INFLATER_SERVICE);
- //獲得系統的布局過濾服務
- myLayout2=(LinearLayout) layoutInflater2.inflate(R.layout.roundprogress, null);
- //得到事先設計好的布局
- AlterD2.setTitle(getResources().getString(R.string.RoundO));
- //設置對話框標題
- AlterD2.setIcon(R.drawable.ma);
- //設置對話框圖標
- AlterD2.setMessage(getResources().getString(R.string.ADDView));
- //設置對話框提示信息
- AlterD2.setView(myLayout2);
- //設置對話框中的View
- AlterD2.show();
- //讓對話框顯示
AlertDialog中的長形ProgressBar(可控制)
①先來設計一個Layout,待會兒作為一個View,加入AlertDialog.Builder
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_gravity="center_horizontal"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <Button
- android:layout_height="wrap_content"
- android:text="-"
- android:layout_width="50dp"
- android:id="@+id/myView_BT_Down">
- </Button>
- <ProgressBar
- android:layout_gravity="center_vertical"
- android:layout_height="wrap_content"
- style="?android:attr/progressBarStyleHorizontal"
- android:id="@+id/myView_ProgressBar"
- android:progress="57"
- android:layout_width="178dp">
- </ProgressBar>
- <Button android:layout_height="wrap_content"
- android:text="+"
- android:layout_width="50dp"
- android:id="@+id/myView_BT_Up">
- </Button>
- </LinearLayout>
②代碼羅
- private AlertDialog.Builder AlterD,AlterD2;
- //定義提示對話框
- private LayoutInflater layoutInflater;
- //定義布局過濾器
- private LinearLayout myLayout;
- //定義布局
- layoutInflater=(LayoutInflater) getSystemService(this.LAYOUT_INFLATER_SERVICE);
- //獲得系統的布局過濾服務
- myLayout=(LinearLayout) layoutInflater.inflate(R.layout.myview, null);
- //得到事先設計好的布局
- myup=(Button) myLayout.findViewById(R.id.myView_BT_Up);
- mydown=(Button) myLayout.findViewById(R.id.myView_BT_Down);
- mypro=(ProgressBar)myLayout.findViewById(R.id.myView_ProgressBar);
- //通過myLayout.findViewById來獲取自定義View中的Widget控件元素
- myup.setOnClickListener(this);
- //設置對話框View中的按鈕監聽器
- mydown.setOnClickListener(this);
- //設置對話框View中的按鈕監聽器
- mypro.setProgress(Tag);
- //設置一個Tag作為進度值
- AlterD.setTitle(getResources().getString(R.string.RectO));
- //設置對話框標題
- AlterD.setIcon(R.drawable.mb);
- //設置對話框圖標
- AlterD.setMessage(getResources().getString(R.string.ADDView));
- //設置對話框提示信息
- AlterD.setView(myLayout);
- //設置對話框添加的View
- AlterD.setPositiveButton("OK", new DialogInterface.OnClickListener(){
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- MyProgressBar.Tag=mypro.getProgress();
- }});
- //設置對話框按鈕,以及按鈕的事件監聽器
- AlterD.show();
- //讓對話框顯示
③進度條進度值的按鈕事件
- myup.setOnClickListener(this);
- //設置對話框View中的按鈕監聽器
- mydown.setOnClickListener(this);
- //設置對話框View中的按鈕監聽器
- 對應的代碼:
- @Override
- public void onClick(View button) {
- // TODO Auto-generated method stub
- SwitchUPorDown(button);
- }
- private void SwitchUPorDown(View button) {
- switch (button.getId()) {
- case R.id.myView_BT_Up: {
- mypro.incrementProgressBy(1);
- }
- break;
- case R.id.myView_BT_Down: {
- mypro.incrementProgressBy(-1);
- }
- break;
- default:
- break;
- }
- }