1,帶有進度條的ProgressBar
[html]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Request the progress bar to be shown in the title
requestWindowFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.progressbar_1);
setProgressBarVisibility(true);//設定在title裡的ProgressBar可見
final ProgressBar progressHorizontal = (ProgressBar) findViewById(R.id.progress_horizontal);
setProgress(progressHorizontal.getProgress() * 100);//為title中的ProgressBar設定進度
setSecondaryProgress(progressHorizontal.getSecondaryProgress() * 100);//為title中的ProgressBar設定二級進度
Button button = (Button) findViewById(R.id.increase);//一級進度遞增
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
progressHorizontal.incrementProgressBy(1);
// Title progress is in range 0..10000
setProgress(100 * progressHorizontal.getProgress());//為title中的ProgressBar設定進度
}
});
button = (Button) findViewById(R.id.decrease);//一級進度遞減
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
progressHorizontal.incrementProgressBy(-1);
// Title progress is in range 0..10000
setProgress(100 * progressHorizontal.getProgress());//為title中的ProgressBar設定進度
}
});
button = (Button) findViewById(R.id.increase_secondary);//二級進度遞增
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
progressHorizontal.incrementSecondaryProgressBy(1);
// Title progress is in range 0..10000
setSecondaryProgress(100 * progressHorizontal.getSecondaryProgress());
}
});
button = (Button) findViewById(R.id.decrease_secondary);//二級進度遞減
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
progressHorizontal.incrementSecondaryProgressBy(-1);
// Title progress is in range 0..10000
setSecondaryProgress(100 * progressHorizontal.getSecondaryProgress());
}
});
}
設定檔 :
[html]
<ProgressBar android:id="@+id/progress_horizontal"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:max="100"
android:progress="50"
android:secondaryProgress="75" />
:
2, 轉圈的樣式的ProgressBar
[html]
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ProgressBar
android:id="@+android:id/progress_large"
style="?android:attr/progressBarStyleLarge"//大樣式
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ProgressBar //預設
android:id="@+android:id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ProgressBar
android:id="@+android:id/progress_small" //小樣式
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ProgressBar //小標題樣式
android:id="@+android:id/progress_small_title"
style="?android:attr/progressBarStyleSmallTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Java代碼:
[html]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Request for the progress bar to be shown in the title
requestWindowFeature(Window.FEATURE_<span style="color:#ff0000;">INDETERMINATE</span>_PROGRESS);
setContentView(R.layout.progressbar_2);
// Make sure the progress bar is visible
setProgressBarVisibility(true);
}
效果:
3,Dialog樣式的ProgressBar
[html]
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);
dialog.setTitle("Indeterminate");
dialog.setMessage("Please wait while loading...");
dialog.set<span style="color:#ff0000;">Indeterminate</span>(true);//設定轉圈的效果
dialog.setCancelable(true);
return dialog;
}
case DIALOG2_KEY: {
ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage("Please wait while loading...");
dialog.set<span style="color:#ff0000;">Indeterminate</span>(true);//設定轉圈的效果
dialog.setCancelable(true);
return dialog;
}
}
return null;
}
效果:
4,在title上面的轉圈的ProgressBar
[java]
private boolean mToggleIndeterminate = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Request progress bar
requestWindowFeature(Window.FEATURE_<span style="color:#ff0000;">INDETERMINATE</span>_PROGRESS);//轉圈樣的ProgressBar
setContentView(R.layout.progressbar_4);
setProgressBarIndeterminateVisibility(mToggleIndeterminate);//是否可見
Button button = (Button) findViewById(R.id.toggle);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
mToggleIndeterminate = !mToggleIndeterminate;//如果是false,設成true,如果是true設成false.
setProgressBarIndeterminateVisibility(mToggleIndeterminate);//是否可見
}
});
}
作者:johnny901114