1,带有进度条的 ProgressBar
Java 代码:
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);
//设置在 title 里的 ProgressBar 可见
setProgressBarVisibility(true);
final ProgressBar progressHorizontal = (ProgressBar) findViewById(R.id.progress_horizontal);
// 为 title 中的 ProgressBar 设置进度
setProgress(progressHorizontal.getProgress() * 100);
// 为title中的ProgressBar设置二级进度
setSecondaryProgress(progressHorizontal.getSecondaryProgress() * 100);
// 一级进度递增
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());
}
});
}
布局文件 :
<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
Java 代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Request for the progress bar to be shown in the title
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.progressbar_2);
// Make sure the progress bar is visible
setProgressBarVisibility(true);
}
布局文件:
<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>
运行效果如下图所示:
3,Dialog 样式的 ProgressBar
Java 代码:
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.setIndeterminate(true);//设置转圈的效果
dialog.setCancelable(true);
return dialog;
}
case DIALOG2_KEY: {
ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage("Please wait while loading...");
dialog.setIndeterminate(true);//设置转圈的效果
dialog.setCancelable(true);
return dialog;
}
}
return null;
}
程序运行效果如下所示:
4,在 title 上面的转圈的 ProgressBar
private boolean mToggleIndeterminate = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Request progress bar
requestWindowFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.progressbar_4);
// 是否可见
setProgressBarIndeterminateVisibility(mToggleIndeterminate);
Button button = (Button) findViewById(R.id.toggle);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
// 如果是false,设成true,如果是true设成false.
mToggleIndeterminate = !mToggleIndeterminate;
// 是否可见
setProgressBarIndeterminateVisibility(mToggleIndeterminate);
}
});
}
程序运行效果如下图所示: