progressBar详解

来源:互联网 发布:拓普康332n软件 编辑:程序博客网 时间:2024/05/22 08:01

另一篇博文:关于自定义progressBar   简单圆形和水平自定义ProgressBar


ProgressBar滚动体在安卓程序中使用也计较多。

ProgressBar的几个常用属性和方法

Android:max="200"    滚动条最大值
android:progress="0" 滚动条当前值
android:visibility="visible"  滚动条是否可见

setProgress(int) 设置当前值


[html] view plain copy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity" >  
  10.   
  11.     <TextView  
  12.         android:id="@+id/text"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:text="@string/text"  
  16.         android:textSize="25sp" />  
  17.       
  18.     <EditText  
  19.         android:id="@+id/value"  
  20.         android:layout_width="wrap_content"  
  21.         android:layout_height="wrap_content"  
  22.         android:text="2"  
  23.         android:layout_toRightOf="@id/text"  
  24.         android:textSize="15sp" />  
  25.       
  26.     <!-- 定义滚动条  
  27.         sytle滚动条样式:progressBarStyleHorizontal一个长条形  
  28.         max 滚动条最大值  
  29.         progress 滚动条当前值  
  30.         visibility 是否可见  
  31.      -->  
  32.     <ProgressBar  
  33.         android:id="@+id/firstBar"  
  34.         style="?android:attr/progressBarStyleHorizontal"  
  35.         android:layout_width="200dp"  
  36.         android:layout_height="wrap_content"  
  37.         android:layout_alignLeft="@+id/text"  
  38.         android:layout_below="@+id/text"  
  39.         android:max="200"  
  40.         android:maxHeight="48dp"  
  41.         android:minHeight="48dp"  
  42.         android:progress="0"  
  43.         android:visibility="visible" />  
  44.   
  45.     <TextView  
  46.         android:id="@+id/text2"  
  47.         android:layout_below="@id/firstBar"  
  48.         android:layout_width="wrap_content"  
  49.         android:layout_height="wrap_content"  
  50.         android:text="@string/text"  
  51.         android:textSize="25sp" />  
  52.       
  53.     <EditText  
  54.         android:id="@+id/value2"  
  55.         android:layout_below="@id/firstBar"  
  56.         android:layout_width="wrap_content"  
  57.         android:layout_height="wrap_content"  
  58.         android:text="2"  
  59.         android:layout_toRightOf="@id/text"  
  60.         android:textSize="15sp" />  
  61.       
  62.     <!-- 定义滚动条  
  63.         sytle滚动条样式:progressBarStyleLarge一个大圆形样式  
  64.      -->  
  65.     <ProgressBar  
  66.         android:id="@+id/firstBar2"  
  67.         style="?android:attr/progressBarStyleLarge"  
  68.         android:layout_width="200dp"  
  69.         android:layout_height="wrap_content"  
  70.         android:layout_alignLeft="@+id/text2"  
  71.         android:layout_below="@+id/text2"  
  72.         android:max="200"  
  73.         android:progress="0"  
  74.         android:visibility="visible" />  
  75.       
  76.      <!-- 定义一个标题栏的滚动条 
  77.      -->  
  78.     <ProgressBar  
  79.         android:id="@+id/firstBar3"  
  80.         style="?android:attr/progressBarStyleSmallTitle"  
  81.         android:layout_width="200dp"  
  82.         android:layout_height="wrap_content"/>  
  83. </RelativeLayout>  

MainActivity:

处理动态加载滚动条

[java] view plain copy
  1. protected void onCreate(Bundle savedInstanceState) {  
  2.         super.onCreate(savedInstanceState);  
  3.         //请求设置窗口标题栏滚动条  
  4.         requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);  
  5.         setContentView(R.layout.activity_main);  
  6.         pb = (ProgressBar)findViewById(R.id.firstBar);  
  7.         value = (EditText)findViewById(R.id.value);  
  8.         //设置滚动条可见  
  9.         setProgressBarIndeterminateVisibility(true);  
  10.           
  11.         //创建一个Handler  
  12.         mHandler = new Handler(){  
  13.             @Override  
  14.             public void handleMessage(Message msg) {  
  15.                 super.handleMessage(msg);  
  16.                 //处理消息  
  17.                 switch (msg.what) {  
  18.                     case MSG:  
  19.                         //设置滚动条和text的值  
  20.                         pb.setProgress(pro);  
  21.                         value.setText(Integer.toString(pro));  
  22.                         break;  
  23.                     default:  
  24.                         break;  
  25.                 }  
  26.             }  
  27.         };  
  28.         start();  
  29.     }  
  30.       
  31.     private void start()  
  32.     {  
  33.         new Thread(new Runnable() {  
  34.             @Override  
  35.             public void run() {  
  36.                 int max = pb.getMax();  
  37.                 try {  
  38.                     //子线程循环间隔消息  
  39.                     while (pro < max) {  
  40.                         pro += 10;  
  41.                         Message msg = new Message();  
  42.                         msg.what = MSG;  
  43.                         mHandler.sendMessage(msg);  
  44.                         Thread.sleep(1000);  
  45.                     }  
  46.                 } catch (InterruptedException e) {  
  47.                     e.printStackTrace();  
  48.                 }  
  49.             }  
  50.         }).start();  
  51.     }  

效果图:


0 0
原创粉丝点击