现在的位置: 首页 > 综合 > 正文

Android控件之ProgressBar

2012年07月07日 ⁄ 综合 ⁄ 共 4081字 ⁄ 字号 评论关闭

1,带有进度条的ProgressBar

[html] view
plain
copy

  1. protected void onCreate(Bundle savedInstanceState) {  
  2.     super.onCreate(savedInstanceState);  
  3.   
  4.     // Request the progress bar to be shown in the title  
  5.     requestWindowFeature(Window.FEATURE_PROGRESS);  
  6.     setContentView(R.layout.progressbar_1);  
  7.     setProgressBarVisibility(true);//设置在title里的ProgressBar可见  
  8.       
  9.     final ProgressBar progressHorizontal = (ProgressBar) findViewById(R.id.progress_horizontal);  
  10.       
  11.     setProgress(progressHorizontal.getProgress() * 100);//为title中的ProgressBar设置进度  
  12.     setSecondaryProgress(progressHorizontal.getSecondaryProgress() * 100);//为title中的ProgressBar设置二级进度  
  13.       
  14.     Button button = (Button) findViewById(R.id.increase);//一级进度递增  
  15.     button.setOnClickListener(new Button.OnClickListener() {  
  16.         public void onClick(View v) {  
  17.             progressHorizontal.incrementProgressBy(1);  
  18.             // Title progress is in range 0..10000  
  19.             setProgress(100 * progressHorizontal.getProgress());//为title中的ProgressBar设置进度  
  20.         }  
  21.     });  
  22.   
  23.     button = (Button) findViewById(R.id.decrease);//一级进度递减  
  24.     button.setOnClickListener(new Button.OnClickListener() {  
  25.         public void onClick(View v) {  
  26.             progressHorizontal.incrementProgressBy(-1);  
  27.             // Title progress is in range 0..10000  
  28.             setProgress(100 * progressHorizontal.getProgress());//为title中的ProgressBar设置进度  
  29.         }  
  30.     });  
  31.   
  32.     button = (Button) findViewById(R.id.increase_secondary);//二级进度递增  
  33.     button.setOnClickListener(new Button.OnClickListener() {  
  34.         public void onClick(View v) {  
  35.             progressHorizontal.incrementSecondaryProgressBy(1);  
  36.             // Title progress is in range 0..10000  
  37.             setSecondaryProgress(100 * progressHorizontal.getSecondaryProgress());  
  38.         }  
  39.     });  
  40.   
  41.     button = (Button) findViewById(R.id.decrease_secondary);//二级进度递减  
  42.     button.setOnClickListener(new Button.OnClickListener() {  
  43.         public void onClick(View v) {  
  44.             progressHorizontal.incrementSecondaryProgressBy(-1);  
  45.             // Title progress is in range 0..10000  
  46.             setSecondaryProgress(100 * progressHorizontal.getSecondaryProgress());  
  47.         }  
  48.     });  
  49.       
  50. }  

配置文件 :

[html] view
plain
copy

  1. <ProgressBar android:id="@+id/progress_horizontal"  
  2.     style="?android:attr/progressBarStyleHorizontal"  
  3.     android:layout_width="200dip"  
  4.     android:layout_height="wrap_content"  
  5.     android:max="100"  
  6.     android:progress="50"  
  7.     android:secondaryProgress="75" />  




效果图:


2, 转圈的样式的ProgressBar

[html] view
plain
copy

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="wrap_content"  
  4.     android:orientation="vertical" >  
  5.   
  6.     <ProgressBar  
  7.         android:id="@+android:id/progress_large"  
  8.         style="?android:attr/progressBarStyleLarge"//大样式  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content" />  
  11.   
  12.     <ProgressBar                                  //默认  
  13.         android:id="@+android:id/progress"  
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content" />  
  16.   
  17.     <ProgressBar  
  18.         android:id="@+android:id/progress_small"   //小样式  
  19.         style="?android:attr/progressBarStyleSmall"  
  20.         android:layout_width="wrap_content"  
  21.         android:layout_height="wrap_content" />  
  22.   
  23.     <ProgressBar                                   //小标题样式  
  24.         android:id="@+android:id/progress_small_title"  
  25.         style="?android:attr/progressBarStyleSmallTitle"  
  26.         android:layout_width="wrap_content"  
  27.         android:layout_height="wrap_content" />  
  28.   
  29. </LinearLayout>  


Java代码:

[html] view
plain
copy

  1. protected void onCreate(Bundle savedInstanceState) {  
  2.     super.onCreate(savedInstanceState);  
  3.   
  4.     // Request for the progress bar to be shown in the title  
  5.     requestWindowFeature(Window.FEATURE_<span style="color:#ff0000;">INDETERMINATE</span>_PROGRESS);  
  6.       
  7.     setContentView(R.layout.progressbar_2);  
  8.       
  9.     // Make sure the progress bar is visible  

抱歉!评论已关闭.