38
技術社區[雲棲]
API Demos 2.3 學習筆記 (14)-- Views->Progress Bar
更多精彩內容,請點擊閱讀:《API Demos 2.3 學習筆記》
進度條ProgressBar可以用來顯示某項操作(比如下載文件)的當前進度。ProgressBar主要有兩種模式:可以隨時確定當前進度值的模式(progressBarStyleHorizontal風格),無法確定當前進度值的模式(其他7種風格)。另外,ProgressBar還有8種係統內置的風格:
- progressBarStyle 默認風格
- progressBarStyleHorizontal 水平進度條風格
- progressBarStyleInverse 反色中等大小風格
- progressBarStyleLarge 超大風格
- progressBarStyleLargeInverse 反色超大風格
- progressBarStyleSmall 超小風格
- progressBarStyleSmallInverse 反色超小風格
- progressBarStyleSmallTitle 標題小風格
這些風格的ProgressBar預覽效果如下所示:

以上幾種風格的設置方法是在xml布局文件中ProgressBar控件內設置style(默認風格progressBarStyle可以不標明):
ProgressBar不僅可以放在主界麵中,而且還能放在標題欄中,甚至還可以放在對話框中。這裏我們為了便於理解,按照ProgressBar的模式和用途分成六種情況進行介紹。
1、主界麵中的水平進度條
<ProgressBar android: android:layout_width="120dip" android:layout_height="wrap_content" android:max="100" android:progress="50" android:secondaryProgress="75" />注:android:max="100" 屬性表示該ProgressBar 的最大值為100,即取值範圍為0~100
android:progress="50" 屬性表示當前初始的進度值為50
android:secondaryProgress="75" 屬性表示當前初始的第二進度值為 75
2、主界麵中的環形進度條
<ProgressBar android: android:layout_width="wrap_content" android:layout_height="wrap_content" />
注:style可以取值除了 progressBarStyleHorizontal之外的另外7個風格取值。
3、標題欄中的水平進度條
//請求窗口特色風格,這裏設置成明確的進度風格
requestWindowFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
//設置標題欄上ProgressBar的主要進度值和第二進度值
//標題欄上ProgressBar的取值範圍為0~10000
setProgress(5000);
setSecondaryProgress(7500);
//顯示標題上的進度條(確定進度值)
setProgressBarVisibility(true);
注:1、requestWindowFeature(Window.FEATURE_PROGRESS);一句必須放在 setContentView(R.layout.main);之前,否則程序報錯。2、setProgressBarVisibility(true);用來設置進度條是否可見。setProgress(5000);和setSecondaryProgress(7500); 分別用來設置進度條的主要進度值和第二進度值。 這幾句必須放在 setContentView(R.layout.main);之後,否則程序報錯。
4、標題欄中的環形進度條
//請求窗口特色風格,這裏設置成不明確的進度風格
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.main);
//顯示標題上的進度條(不確定進度值)
setProgressBarIndeterminateVisibility(true);注:1、 requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); 一句必須放在 setContentView(R.layout.main);之前,否則程序報錯。2、 setProgressBarIndeterminateVisibility(true);用來設置進度條是否可見。這一句必須放在 setContentView(R.layout.main);之後,否則程序報錯。
5、進度對話框中的水平進度條
6、進度對話框中的環形進度條
//創建ProgressDialog
final ProgressDialog mProgressDialog = new ProgressDialog(this);
//設置ProgressDialog對象類型為環形
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//設置ProgressDialog的圖標
mProgressDialog.setIcon(R.drawable.icon);
//設置ProgressDialog的標題
mProgressDialog.setTitle("ProgressDialog");
//設置ProgressDialog的消息
mProgressDialog.setMessage("Loading...");
//設置ProgressDialog中進度條最大值100,即取值範圍為0~100
mProgressDialog.setMax(100);
//設置ProgressDialog中進度條當前初始主要進度值為 50
mProgressDialog.setProgress(0);
//設置ProgressDialog中進度條當前初始第二進度值為 75
mProgressDialog.setSecondaryProgress(0);
//設置ProgressDialog的當前進度值是否不確定
mProgressDialog.setIndeterminate(false);
//設置ProgressDialog是否可以通過回退鍵取消
mProgressDialog.setCancelable(true);
//設置ProgressDialog的確定按鈕以及監聽器
mProgressDialog.setButton( DialogInterface.BUTTON_POSITIVE,"Ok", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
// TODO 執行Button_OK按鈕點擊後的響應動作
mProgressDialog.dismiss();
}
});
//設置ProgressDialog的取消按鈕以及監聽器
mProgressDialog.setButton(DialogInterface.BUTTON_NEGATIVE,"Cancel", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
// TODO 執行Button_CANCEL按鈕點擊後的響應動作
mProgressDialog.dismiss();
}
});
//顯示ProgressDialog
mProgressDialog.show();注:直接調用setProgress和setSecondaryProgress無法真正改變進度條的主進度值和第二進度值。關於如何修改進度值,請點擊閱讀:《android dialog ——ProgressDialog 進度條對話框詳解》6、進度對話框中的環形進度條
//創建ProgressDialog
final ProgressDialog mProgressDialog = new ProgressDialog(this);
//設置ProgressDialog對象類型為環形
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
//設置ProgressDialog的圖標
mProgressDialog.setIcon(R.drawable.icon);
//設置ProgressDialog的標題
mProgressDialog.setTitle("ProgressDialog");
//設置ProgressDialog的消息
mProgressDialog.setMessage("Loading...");
//設置ProgressDialog的當前進度值是否不確定
mProgressDialog.setIndeterminate(true);
//設置ProgressDialog是否可以通過回退鍵取消
mProgressDialog.setCancelable(true);
//設置ProgressDialog的確定按鈕以及監聽器
mProgressDialog.setButton( DialogInterface.BUTTON_POSITIVE,"Ok", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
// TODO 執行Button_OK按鈕點擊後的響應動作
mProgressDialog.dismiss();
}
});
//設置ProgressDialog的取消按鈕以及監聽器
mProgressDialog.setButton(DialogInterface.BUTTON_NEGATIVE,"Cancel", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
// TODO 執行Button_CANCEL按鈕點擊後的響應動作
mProgressDialog.dismiss();
}
});
//顯示ProgressDialog
mProgressDialog.show();
下麵我們進行實例代碼解析:
1、Incremental
res-value-string.xml
1、Incremental
res-value-string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="progressbar_1_plus">+</string>
<string name="progressbar_1_minus">-</string>
<string name="progressbar_1_default_progress">Default progress:</string>
<string name="progressbar_1_secondary_progress">Secondary progress:</string>
</resources>
res-layout-progressbar_1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- 一個ProgressBar對象,類型為:progressBarStyleHorizontal -->
<ProgressBar android:
android:layout_width="200dip"
android:layout_height="wrap_content"
android:max="100"
android:progress="50"
android:secondaryProgress="75" />
<!-- 一個TextView對象,提示下麵兩個按鈕改變的主要進度值-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/progressbar_1_default_progress" />
<!-- 水平布局內放置兩個按鈕,分別用於增大和減小進度條的主要進度值 -->
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button android:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/progressbar_1_minus" />
<Button android:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/progressbar_1_plus" />
</LinearLayout>
<!-- 一個TextView對象,提示下麵兩個按鈕改變的第二進度值-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/progressbar_1_secondary_progress" />
<!-- 水平布局內放置兩個按鈕,分別用於增大和減小進度條的第二進度值 -->
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button android:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/progressbar_1_minus" />
<Button android:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/progressbar_1_plus" />
</LinearLayout>
</LinearLayout>
src-com.example.android.apis.view-ProgressBar1.java
package com.example.android.apis.view;
import com.example.android.apis.R;
import android.app.Activity;
import android.widget.Button;
import android.widget.ProgressBar;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
/**
* 演示如何在窗口標題欄中使用進度條。
* 進度條將會一直顯示,直到進度完成,此時進度條消失。
*/
public class ProgressBar1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 請求在標題欄中顯示進度條
requestWindowFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.progressbar_1);
//設置標題欄中的進度條可見
setProgressBarVisibility(true);
// 通過findViewById方法獲得一個ProgressBar對象
final ProgressBar progressHorizontal = (ProgressBar) findViewById(R.id.progress_horizontal);
//獲取ProgressBar對象(取值範圍0~100)的主要進度值和第二進度值,經過轉換後設置到標題欄中的ProgressBar對象(取值範圍0~10000)。
setProgress(progressHorizontal.getProgress() * 100);
setSecondaryProgress(progressHorizontal.getSecondaryProgress() * 100);
//該按鈕用於增大ProgressBar的主要進度值,步進值為1
Button button = (Button) findViewById(R.id.increase);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
//progressHorizontal主要進度值加1
progressHorizontal.incrementProgressBy(1);
//標題欄中的進度條取值範圍0~10000,而progressHorizontal取值範圍0~100,因此,獲取progressHorizontal進度值,乘以100,設置成標題欄上進度條的進度值。下同。
setProgress(100 * progressHorizontal.getProgress());
}
});
//該按鈕用於減小ProgressBar的主要進度值,步進值為1
button = (Button) findViewById(R.id.decrease);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
//progressHorizontal主要進度值減1
progressHorizontal.incrementProgressBy(-1);
setProgress(100 * progressHorizontal.getProgress());
}
});
//該按鈕用於增大ProgressBar的第二進度值,步進值為1
button = (Button) findViewById(R.id.increase_secondary);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
//progressHorizontal第二進度值加1
progressHorizontal.incrementSecondaryProgressBy(1);
//標題欄中的進度條取值範圍0~10000,而progressHorizontal取值範圍0~100,因此,獲取progressHorizontal第二進度值,乘以100,設置成標題欄上進度條的第二進度值。下同。
setSecondaryProgress(100 * progressHorizontal.getSecondaryProgress());
}
});
//該按鈕用於減小ProgressBar的第二進度值,步進值為1
button = (Button) findViewById(R.id.decrease_secondary);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
//progressHorizontal第二進度值減1
progressHorizontal.incrementSecondaryProgressBy(-1);
setSecondaryProgress(100 * progressHorizontal.getSecondaryProgress());
}
});
}
}
2、Smooth
res-layout-progressbar_2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- 一個ProgressBar對象,類型為:progressBarStyleLarge -->
<ProgressBar android:
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!-- 一個ProgressBar對象,類型為默認類型:progressBarStyle -->
<ProgressBar android:
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!-- 一個ProgressBar對象,類型為默認類型:progressBarStyleSmall -->
<ProgressBar android:
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!-- 一個ProgressBar對象,類型為默認類型:progress_small_title -->
<ProgressBar android:
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
src-com.example.android.apis.view-ProgressBar2.java
package com.example.android.apis.view;
import com.example.android.apis.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
/**
* 演示如何在窗口標題欄中使用不確定進度的進度條。
* 本實例演示了3中不同大小的環形進度條,
*/
public class ProgressBar2 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 請求在標題欄中顯示不確定進度類型的進度條
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.progressbar_2);
// 設置標題欄中的進度條可見
setProgressBarIndeterminateVisibility(true);
}
}
3、Dialogs
res-value-string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="progressbar_3_progress">Show Progress</string>
<string name="progressbar_3_indeterminate">Show Indeterminate</string>
<string name="progressbar_3_indeterminate_no_title">Show Indeterminate No Title</string>
</resources>
res-layout-progressbar_3.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- 一個Button對象,點擊顯示一個帶有標題的ProgressDialog -->
<Button android:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/progressbar_3_indeterminate" />
<!-- 一個Button對象,點擊顯示一個不帶標題的ProgressDialog -->
<Button android:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/progressbar_3_indeterminate_no_title" />
</LinearLayout>
package com.example.android.apis.view;
import com.example.android.apis.R;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
/**
* 演示如何使用進度對話框
*/
public class ProgressBar3 extends Activity {
ProgressDialog mDialog1;
ProgressDialog mDialog2;
private static final int DIALOG1_KEY = 0;
private static final int DIALOG2_KEY = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.progressbar_3);
Button button = (Button) findViewById(R.id.showIndeterminate);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(DIALOG1_KEY); //顯示帶有標題的進度對話框
}
});
button = (Button) findViewById(R.id.showIndeterminateNoTitle);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(DIALOG2_KEY); //顯示不帶標題的進度對話框
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG1_KEY: {
ProgressDialog dialog = new ProgressDialog(this); //創建ProgressDialog
dialog.setTitle("Indeterminate"); //設置標題
dialog.setMessage("Please wait while loading..."); //設置主題信息
dialog.setIndeterminate(true); //設置進度條為不明確進度的類型(環形)
dialog.setCancelable(true); //設置窗口可以通過退回鍵取消
return dialog;
}
case DIALOG2_KEY: {
ProgressDialog dialog = new ProgressDialog(this); //創建ProgressDialog
dialog.setMessage("Please wait while loading..."); //設置主題信息
dialog.setIndeterminate(true); //設置進度條為不明確進度的類型(環形)
dialog.setCancelable(true); //設置窗口可以通過退回鍵取消
return dialog;
}
}
return null;
}
}
4、In Title Bar
res-value-string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="progressbar_4_toggle">Toggle Indeterminate</string>
</resources>
res-layout-progressbar_4.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- 一個Button對象,點擊顯示/隱藏標題欄上的進度條 -->
<Button android:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/progressbar_4_toggle" />
</LinearLayout>
src-com.example.android.apis.view-ProgressBar4.java
package com.example.android.apis.view;
import com.example.android.apis.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.View;
import android.widget.Button;
/**
* 演示如何在標題欄中使用不確定進度值的進度條
*/
public class ProgressBar4 extends Activity {
private boolean mToggleIndeterminate = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 請求在標題欄中顯示不確定進度類型的進度條
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.progressbar_4);
// 設置標題欄中的進度條是否可見
setProgressBarIndeterminateVisibility(mToggleIndeterminate);
//點擊一次,改變一次mToggleIndeterminate值
Button button = (Button) findViewById(R.id.toggle);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
mToggleIndeterminate = !mToggleIndeterminate;
setProgressBarIndeterminateVisibility(mToggleIndeterminate);
}
});
}
}
知識點1:關於Progress的自定義等知識,請點擊閱讀:《Android自定義ProgressDialog的方法》,《自定義ProgressBar》以及《多式樣ProgressBar》。
預覽效果;


最後更新:2017-04-02 06:51:56