Android控件之ProgressBar

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" />  



效果图:

Android控件之ProgressBar_第1张图片


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  
  10.     setProgressBarVisibility(true);  
  11. }  

效果:

Android控件之ProgressBar_第2张图片

3,Dialog样式的ProgressBar

[html]  view plain copy
  1. private static final int DIALOG1_KEY = 0;  
  2.    private static final int DIALOG2_KEY = 1;  
  3.   
  4.   
  5.    @Override  
  6.    protected void onCreate(Bundle savedInstanceState) {  
  7.        super.onCreate(savedInstanceState);  
  8.   
  9.        setContentView(R.layout.progressbar_3);  
  10.   
  11.        Button button = (Button) findViewById(R.id.showIndeterminate);  
  12.        button.setOnClickListener(new View.OnClickListener() {  
  13.            public void onClick(View v) {  
  14.                showDialog(DIALOG1_KEY);  
  15.            }  
  16.        });  
  17.   
  18.        button = (Button) findViewById(R.id.showIndeterminateNoTitle);  
  19.        button.setOnClickListener(new View.OnClickListener() {  
  20.            public void onClick(View v) {  
  21.                showDialog(DIALOG2_KEY);  
  22.            }  
  23.        });  
  24.    }  
  25.   
  26.    @Override  
  27.    protected Dialog onCreateDialog(int id) {  
  28.        switch (id) {  
  29.            case DIALOG1_KEY: {  
  30.                ProgressDialog dialog = new ProgressDialog(this);  
  31.                dialog.setTitle("Indeterminate");  
  32.                dialog.setMessage("Please wait while loading...");  
  33.                dialog.set<span style="color:#ff0000;">Indeterminate</span>(true);//设置转圈的效果  
  34.                dialog.setCancelable(true);  
  35.                return dialog;  
  36.            }  
  37.            case DIALOG2_KEY: {  
  38.                ProgressDialog dialog = new ProgressDialog(this);  
  39.                dialog.setMessage("Please wait while loading...");  
  40.                dialog.set<span style="color:#ff0000;">Indeterminate</span>(true);//设置转圈的效果  
  41.                dialog.setCancelable(true);  
  42.                return dialog;  
  43.            }  
  44.        }  
  45.        return null;  
  46.    }  

效果:

Android控件之ProgressBar_第3张图片Android控件之ProgressBar_第4张图片


4,在title上面的转圈的ProgressBar

[java]  view plain copy
  1. private boolean mToggleIndeterminate = false;  
  2.   
  3. @Override  
  4. protected void onCreate(Bundle savedInstanceState) {  
  5.     super.onCreate(savedInstanceState);  
  6.   
  7.     // Request progress bar  
  8.     requestWindowFeature(Window.FEATURE_<span style="color:#ff0000;">INDETERMINATE</span>_PROGRESS);//转圈样的ProgressBar  
  9.     setContentView(R.layout.progressbar_4);  
  10.     setProgressBarIndeterminateVisibility(mToggleIndeterminate);//是否可见  
  11.       
  12.     Button button = (Button) findViewById(R.id.toggle);  
  13.     button.setOnClickListener(new Button.OnClickListener() {  
  14.         public void onClick(View v) {  
  15.             mToggleIndeterminate = !mToggleIndeterminate;//如果是false,设成true,如果是true设成false.  
  16.             setProgressBarIndeterminateVisibility(mToggleIndeterminate);//是否可见  
  17.         }  
  18.     });  
  19. }  
Android控件之ProgressBar_第5张图片 Android控件之ProgressBar_第6张图片

你可能感兴趣的:(Android控件之ProgressBar)