ProgressBar--进度条
默认情况下,如果简单的在xml布局文件中,通过<ProgressBar>标签创建出的ProgressBar是圆形进度条样式,如下图所示:
但是通常情况下,我们一般使用的是横向条形进度条,这样就需要给ProgressBar设置不同的样式,ProgressBar可选的样式如下:
1 Widget.ProgressBar.Horizontal
2 Widget.ProgressBar.Small
3 Widget.ProgressBar.Large
4 Widget.ProgressBar.Inverse
5 Widget.ProgressBar.Small.Inverse
6 Widget.ProgressBar.Large.Inverse
当我们将Widget.ProgressBar.Horizontal的样式设置到ProgressBar标签时,代码如下:
<ProgressBar android:layout_width="match_parent" android:layout_height="30dp" android:progress="50" style="?android:attr/progressBarStyleHorizontal" />
在MainActivity中,我们可以使用ProgressBar的setProgress(int progress)和incrementProgressBy(int diff)动态的设置ProgressBar的进度,如下所示:
pb = (ProgressBar) findViewById(R.id.progressBar_Main); pb.incrementProgressBy(1); pb.setProgress(pb.getProgress() + 10);
xml布局文件如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <ProgressBar android:id="@+id/progressBar_Main" android:layout_width="match_parent" android:layout_height="wrap_content" style="?android:attr/progressBarStyleHorizontal" android:progress="50" android:secondaryProgress="70"/> <Button android:id="@+id/increase_progress" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="一级菜单增加" android:onClick="btnClicked"/> <Button android:id="@+id/decrease_progress" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="一级菜单减少" android:onClick="btnClicked"/> <Button android:id="@+id/increase_secondaryProgress" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="二级菜单增加" android:onClick="btnClicked"/> <Button android:id="@+id/decrease_secondaryProgress" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="二级菜单减少" android:onClick="btnClicked"/> </LinearLayout>
下面是MainActivity中的代码,如何动态的设置一级和二级进度:
package com.danny_jiang.day04_progressbar; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.ProgressBar; public class MainActivity extends Activity { private ProgressBar pb; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); pb = (ProgressBar) findViewById(R.id.progressBar_Main); } public void btnClicked(View view) { switch (view.getId()) { case R.id.increase_progress: pb.incrementProgressBy(1); pb.setProgress(pb.getProgress() + 10); break; case R.id.decrease_progress: pb.setProgress(pb.getProgress() - 10); break; case R.id.increase_secondaryProgress: pb.setSecondaryProgress(pb.getSecondaryProgress() + 10); break; case R.id.decrease_secondaryProgress: pb.setSecondaryProgress(pb.getSecondaryProgress() - 10); break; default: break; } } }