<p>ProgressBar是Android的进度条。体验效果</p><p><img src="/up_pic/201812/191110344496.png" title="191110344496.png" alt="1.png"/></p><p>下面详细介绍ProgressBar</p><p>一、说明</p><p> 在某些操作的进度中的可视指示器,为用户呈现操作的进度,还它有一个次要的进度条,用来显示中间进度,如在流媒体播放的缓冲区的进度。一个进度条也可不确定其进度。在不确定模式下,进度条显示循环动画。这种模式常用于应用程序使用任务的长度是未知的。</p><p>二、XML重要属性</p><p> android:progressBarStyle:默认进度条样式</p><p> android:progressBarStyleHorizontal:水平样式</p><p>三、重要方法</p><p> getMax():返回这个进度条的范围的上限</p><p> getProgress():返回进度</p><p> getSecondaryProgress():返回次要进度</p><p> incrementProgressBy(int diff):指定增加的进度</p><p> isIndeterminate():指示进度条是否在不确定模式下</p><p> setIndeterminate(boolean indeterminate):设置不确定模式下</p><p> setVisibility(int v):设置该进度条是否可视</p><p>四、重要事件</p><p> onSizeChanged(int w, int h, int oldw, int oldh):当进度值改变时引发此事件</p><p>五、实例</p><p>1.布局文件</p><pre class="brush:xml;toolbar:false"><?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ProgressBarandroid: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"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="默认进度条"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Buttonandroid:id="@+id/decrease"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="减少"/>
<Buttonandroid:id="@+id/increase"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="增加"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自定义进度条"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Buttonandroid:id="@+id/decrease_secondary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第二减少"/>
<Buttonandroid:id="@+id/increase_secondary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第二增加"/>
</LinearLayout>
</LinearLayout></pre><p></p><p>2.Java代码</p><pre class="brush:java;toolbar:false">packagewjq.WidgetDemo;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.view.Window;
importandroid.widget.Button;
importandroid.widget.ProgressBar;
publicclassProgressBarDemoextendsActivity{
/*(non-Javadoc)
*@seeandroid.app.Activity#onCreate(android.os.Bundle)
*/
@Override
protectedvoidonCreate(BundlesavedInstanceState){
//TODOAuto-generatedmethodstub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.probarpage);
setProgressBarVisibility(true);
finalProgressBarprogressHorizontal=(ProgressBar)findViewById(R.id.progress_horizontal);
setProgress(progressHorizontal.getProgress()*100);
setSecondaryProgress(progressHorizontal.getSecondaryProgress()*100);
Buttonbutton=(Button)findViewById(R.id.increase);
button.setOnClickListener(newButton.OnClickListener(){
publicvoidonClick(Viewv){
progressHorizontal.incrementProgressBy(1);
//Titleprogressisinrange0..10000
setProgress(100*progressHorizontal.getProgress());
}
});
button=(Button)findViewById(R.id.decrease);
button.setOnClickListener(newButton.OnClickListener(){
publicvoidonClick(Viewv){
progressHorizontal.incrementProgressBy(-1);
//Titleprogressisinrange0..10000
setProgress(100*progressHorizontal.getProgress());
}
});
button=(Button)findViewById(R.id.increase_secondary);
button.setOnClickListener(newButton.OnClickListener(){
publicvoidonClick(Viewv){
progressHorizontal.incrementSecondaryProgressBy(1);
//Titleprogressisinrange0..10000
setSecondaryProgress(100*progressHorizontal.getSecondaryProgress());
}
});
button=(Button)findViewById(R.id.decrease_secondary);
button.setOnClickListener(newButton.OnClickListener(){
publicvoidonClick(Viewv){
progressHorizontal.incrementSecondaryProgressBy(-1);
//Titleprogressisinrange0..10000
setSecondaryProgress(100*progressHorizontal.getSecondaryProgress());
}
});
}
}</pre>