Android 控件之ProgressBar进度条

<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">&lt;?xmlversion=&quot;1.0&quot;encoding=&quot;utf-8&quot;?&gt; &lt;LinearLayoutxmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; android:orientation=&quot;vertical&quot; android:layout_width=&quot;match_parent&quot; android:layout_height=&quot;wrap_content&quot;&gt; &lt;ProgressBarandroid:id=&quot;@+id/progress_horizontal&quot; style=&quot;?android:attr/progressBarStyleHorizontal&quot; android:layout_width=&quot;200dip&quot; android:layout_height=&quot;wrap_content&quot; android:max=&quot;100&quot; android:progress=&quot;50&quot; android:secondaryProgress=&quot;75&quot;/&gt; &lt;TextView android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot; android:text=&quot;默认进度条&quot;/&gt; &lt;LinearLayout android:orientation=&quot;horizontal&quot; android:layout_width=&quot;match_parent&quot; android:layout_height=&quot;wrap_content&quot;&gt; &lt;Buttonandroid:id=&quot;@+id/decrease&quot; android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot; android:text=&quot;减少&quot;/&gt; &lt;Buttonandroid:id=&quot;@+id/increase&quot; android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot; android:text=&quot;增加&quot;/&gt; &lt;/LinearLayout&gt; &lt;TextView android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot; android:text=&quot;自定义进度条&quot;/&gt; &lt;LinearLayout android:orientation=&quot;horizontal&quot; android:layout_width=&quot;match_parent&quot; android:layout_height=&quot;wrap_content&quot;&gt; &lt;Buttonandroid:id=&quot;@+id/decrease_secondary&quot; android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot; android:text=&quot;第二减少&quot;/&gt; &lt;Buttonandroid:id=&quot;@+id/increase_secondary&quot; android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot; android:text=&quot;第二增加&quot;/&gt; &lt;/LinearLayout&gt; &lt;/LinearLayout&gt;</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>
RangeTime:0.161074s
RangeMem:211.6 KB
返回顶部 留言