UI组件之 ProgressBar及其子类(一)ProgressBar进度条的使用
ProgressBar本身進度條組件,它派生了:SeekBar和RatingBar兩個組件,他們的繼承關系如下:
1、ProgressBar有兩個進度,一個是android:progress,另一個是android:secondaryProgress。后者主要是為緩存需要所涉及的,比如在看網絡視頻時候都會有一個緩存的進度條以及還要一個播放的進度,在這里緩存的進度就可以是android:secondaryProgress,而播放進度就是android:progress,有了secondProgress,可以很方便定制ProgressBar。
2、ProgressBar分為確定的和不確定的,確定的是我們能明確看到進度,相反不確定的就是不清楚、不確定一個操作需要多長時間來完成,這個時候就需要用的不確定的ProgressBar了。屬性android:indeterminate如果設置為true的話,那么ProgressBar就可能是圓形的滾動條或者水平的滾動條(由樣式決定),但是我們一般時候,是直接使用Style類型來區分圓形還是水平ProgressBar的。
進度條的常用xml屬性如下:
其中根據Style屬性的設置不同,可以指定ProgressBar的3種環形進度條和1中水平進度條:
1,大環形進度條 ?style="?android:attr/progressBarStyleLarge或者style="?android:attr/progressBarStyleLargeInverse"
2,普通環形進度條:style="android:progressBarStyle"
3,小環形進度條:style="?android:attr/progressBarStyleSmall"
4,普通水平進度條:style=“?android:attr/progressBarStyleHorizontal”
常用屬性解釋:
android:max:設置進度條的最大值
android:progress:設置已完成進度條的進度值
android:progressDrawable:設置進度條軌道對應的Drawable對象??梢允且粋€LayerDrawable對象
android:indeterminate:設置屬性是true,設置進度條不精確顯示的進度,就是環形進度條的進度值
android:indeterminateDrawable:設置繪制不精確顯示的精度的Drawable對象,就是環形進度的軌道Drawable對象;
android:indeterminateDuration;設置不精確顯示精度的持續時間
mybar.xml
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" ><!-- 設置軌道背景 --><item android:id="@android:id/background" android:drawable="@drawable/no"></item><!-- 設置軌道上已完成背景的樣式 ,在上面--><item android:id="@android:id/progress" android:drawable="@drawable/ok"></item> </layer-list>
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/root"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content" ><!-- 大環形進度條 style="?android:attr/progressBarStyleLarge"--><ProgressBarandroid:id="@+id/progressBar1"style="?android:attr/progressBarStyleLargeInverse"android:layout_width="wrap_content"android:layout_height="wrap_content" /><!-- 普通的中等進度條 --><ProgressBarandroid:id="@+id/progressBar2"android:layout_width="wrap_content"android:layout_height="wrap_content" /><!-- 小環形進度條 style="?android:attr/progressBarStyleSmall"--><ProgressBarandroid:id="@+id/ProgressBar3"style="?android:attr/progressBarStyleSmall"android:layout_width="wrap_content"android:layout_height="wrap_content" /></LinearLayout><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="任務完成的進度"android:textSize="20dp" /><!-- 普通的水平進度條 --><ProgressBarandroid:id="@+id/progressBar4"style="?android:attr/progressBarStyleHorizontal"android:layout_width="fill_parent"android:layout_height="wrap_content"android:max="100"android:progress="0"android:visibility="visible" /><!-- 水平進度條,改變軌道外觀 ,外觀圖片是在drawable里面,xml文件的layer-list元素編寫--><ProgressBarandroid:id="@+id/progressBar5"style="?android:attr/progressBarStyleHorizontal"android:layout_width="fill_parent"android:layout_height="wrap_content"android:max="100"android:paddingTop="20dp"android:progressDrawable="@drawable/mybar" /></LinearLayout> MainActivity.java
package com.hust.progresbartest;import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.Menu; import android.view.MenuItem; import android.widget.ProgressBar;public class MainActivity extends Activity {/*給程序模擬填充長度為100的數組*/private int[] data=new int[100];int hasData=0;//記錄ProgressBar的完成進度int status=0;ProgressBar bar,bar2;//創建一個負責更新進度的HandlerHandler h=new Handler(){public void handleMessage(Message msg){if(msg.what==0x111){//進度條設置已完成的值bar.setProgress(status);bar2.setProgress(status); }}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);bar=(ProgressBar) findViewById(R.id.progressBar4);bar2=(ProgressBar) findViewById(R.id.progressBar5);//啟動線程來執行任務new Thread(){public void run(){while(status<100){//獲取耗時操作完成百分比status=dowork();//發送消息h.sendEmptyMessage(0x111);}}}.start();}//模擬一個耗時的操作public int dowork(){//為數組元素賦值data[hasData++]=(int)(Math.random()*100);try{Thread.sleep(100);}catch(InterruptedException e){e.printStackTrace();}return hasData;}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId();if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);} }
總結
以上是生活随笔為你收集整理的UI组件之 ProgressBar及其子类(一)ProgressBar进度条的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: UI组件之AdapterView及其子类
- 下一篇: UI组件之ProgressBar及其子类