ProgressBar 各种样式

 

多式样ProgressBar
普通圆形ProgressBar

该类型进度条也就是一个表示运转的过程,例如发送短信,连接网络等等,表示一个过程正在执行中。

一般只要在XML布局中定义就可以了。

Java代码 复制代码 收藏代码
  1. <progressBar android:id="@+id/widget43"  
  2.       android:layout_width="wrap_content"   
  3.       android:layout_height="wrap_content"      
  4.       android:layout_gravity="center_vertical">  
  5. </ProgressBar>  
<progressBar android:id="@+id/widget43"
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"    
      android:layout_gravity="center_vertical">
</ProgressBar>


此时,没有设置它的风格,那么它就是圆形的,一直会旋转的进度条。

各大小样式圆形ProgressBar
超大号圆形ProgressBar

此时,给设置一个style风格属性后,该ProgressBar就有了一个风格,
这里大号ProgressBar的风格是:
Java代码 复制代码 收藏代码
  1. style="?android:attr/progressBarStyleLarge"  
 style="?android:attr/progressBarStyleLarge"
完整

XML定义是:

Java代码 复制代码 收藏代码
  1.  <progressBar android:id="@+id/widget196"  
  2.       android:layout_width="wrap_content"   
  3.       android:layout_height="wrap_content"  
  4.       style="?android:attr/progressBarStyleLarge">  
  5. </ProgressBar>  
 <progressBar android:id="@+id/widget196"
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"
      style="?android:attr/progressBarStyleLarge">
</ProgressBar>



小号圆形ProgressBar

小号ProgressBar对应的风格是:
Java代码 复制代码 收藏代码
  1. style="?android:attr/progressBarStyleSmall"  
 style="?android:attr/progressBarStyleSmall"

完整XML定义是:

Java代码 复制代码 收藏代码
  1. <progressBar android:id="@+id/widget108"  
  2.       android:layout_width="wrap_content"   
  3.       android:layout_height="wrap_content"  
  4.       style="?android:attr/progressBarStyleSmall">  
  5. </ProgressBar>  
<progressBar android:id="@+id/widget108"
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"
      style="?android:attr/progressBarStyleSmall">
</ProgressBar>



标题型圆形ProgressBar

标题型ProgressBar对应的风格是:
Java代码 复制代码 收藏代码
  1. style="?android:attr/progressBarStyleSmallTitle"  
style="?android:attr/progressBarStyleSmallTitle"
完整XML定义是:
Java代码 复制代码 收藏代码
  1. <progressBar android:id="@+id/widget110"  
  2.     android:layout_width="wrap_content"   
  3.     android:layout_height="wrap_content"  
  4.     style="?android:attr/progressBarStyleSmallTitle">  
  5. </ProgressBar>  
<progressBar android:id="@+id/widget110"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    style="?android:attr/progressBarStyleSmallTitle">
</ProgressBar>



代码中实现:

   
Java代码 复制代码 收藏代码
  1. protected void onCreate(Bundle savedInstanceState) {  
  2.   
  3.         // TODO Auto-generated method stub  
  4.   
  5.         super.onCreate(savedInstanceState);  
  6.   
  7.         requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);  
  8.   
  9.         //请求窗口特色风格,这里设置成不明确的进度风格  
  10.   
  11.         setContentView(R.layout.second);  
  12.   
  13.         setProgressBarIndeterminateVisibility(true);  
  14.   
  15.         //设置标题栏中的不明确的进度条是否可以显示  
  16.   
  17.     }  
protected void onCreate(Bundle savedInstanceState) {

        // TODO Auto-generated method stub

        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

        //请求窗口特色风格,这里设置成不明确的进度风格

        setContentView(R.layout.second);

        setProgressBarIndeterminateVisibility(true);

        //设置标题栏中的不明确的进度条是否可以显示

    }


长形进度条

布局中的长形进度条


①首先在XML进行布局
Java代码 复制代码 收藏代码
  1. <progressBar android:id="@+id/progressbar_updown"  
  2.   
  3.         android:layout_width="200dp"   
  4.   
  5.         android:layout_height="wrap_content"  
  6.   
  7.         style="?android:attr/progressBarStyleHorizontal"  
  8.   
  9.         android:layout_gravity="center_vertical"   
  10.   
  11.         android:max="100"  
  12.   
  13.         android:progress="50"  
  14.   
  15.         android:secondaryProgress="70"    />   
<progressBar android:id="@+id/progressbar_updown"

        android:layout_width="200dp" 

        android:layout_height="wrap_content"

        style="?android:attr/progressBarStyleHorizontal"

        android:layout_gravity="center_vertical" 

        android:max="100"

        android:progress="50"

        android:secondaryProgress="70"    /> 


讲解:
Java代码 复制代码 收藏代码
  1. style="?android:attr/progressBarStyleHorizontal"   
style="?android:attr/progressBarStyleHorizontal" 
 
设置风格为长形

Java代码 复制代码 收藏代码
  1. android:max="100"   
android:max="100" 
 
最大进度值为100

Java代码 复制代码 收藏代码
  1. android:progress="50"    
android:progress="50"  

初始化的进度值

Java代码 复制代码 收藏代码
  1. android:secondaryProgress="70"   
android:secondaryProgress="70" 

初始化的底层第二个进度值

Java代码 复制代码 收藏代码
  1. android:layout_gravity="center_vertical"     
android:layout_gravity="center_vertical"   

垂直居中


②代码中运用

Java代码 复制代码 收藏代码
  1. private ProgressBar myProgressBar;  
  2.   
  3. //定义ProgressBar  
  4.   
  5. myProgressBar = (ProgressBar) findViewById(R.id.progressbar_updown);  
  6.   
  7. //ProgressBar通过ID来从XML中获取  
  8.   
  9. myProgressBar.incrementProgressBy(5);  
  10.   
  11. //ProgressBar进度值增加5  
  12.   
  13. myProgressBar.incrementProgressBy(-5);  
  14.   
  15. //ProgressBar进度值减少5  
  16.   
  17. myProgressBar.incrementSecondaryProgressBy(5);  
  18.   
  19. //ProgressBar背后的第二个进度条 进度值增加5  
  20.   
  21. myProgressBar.incrementSecondaryProgressBy(-5);  
  22.   
  23. //ProgressBar背后的第二个进度条 进度值减少5  
  24.    
private ProgressBar myProgressBar;

//定义ProgressBar

myProgressBar = (ProgressBar) findViewById(R.id.progressbar_updown);

//ProgressBar通过ID来从XML中获取

myProgressBar.incrementProgressBy(5);

//ProgressBar进度值增加5

myProgressBar.incrementProgressBy(-5);

//ProgressBar进度值减少5

myProgressBar.incrementSecondaryProgressBy(5);

//ProgressBar背后的第二个进度条 进度值增加5

myProgressBar.incrementSecondaryProgressBy(-5);

//ProgressBar背后的第二个进度条 进度值减少5
 



页面标题中的长形进度条

代码实现:
①先设置一下窗口风格特性

Java代码 复制代码 收藏代码
  1. requestWindowFeature(Window.FEATURE_PROGRESS);  
requestWindowFeature(Window.FEATURE_PROGRESS);


//请求一个窗口进度条特性风格

Java代码 复制代码 收藏代码
  1. setContentView(R.layout.main);  
  2.   
  3. setProgressBarVisibility(true);  
setContentView(R.layout.main);

setProgressBarVisibility(true);


//设置进度条可视

②然后设置进度值

Java代码 复制代码 收藏代码
  1. setProgress(myProgressBar.getProgress() * 100);  
setProgress(myProgressBar.getProgress() * 100);


//设置标题栏中前景的一个进度条进度值

Java代码 复制代码 收藏代码
  1. setSecondaryProgress(myProgressBar.getSecondaryProgress() * 100);  
setSecondaryProgress(myProgressBar.getSecondaryProgress() * 100);


//设置标题栏中后面的一个进度条进度值

//ProgressBar.getSecondaryProgress() 是用来获取其他进度条的进度值


ProgressDialog

ProgressDialog中的圆形进度条
     
ProgressDialog一般用来表示一个系统任务或是开启任务时候的进度,有一种稍等的意思。
代码实现:

Java代码 复制代码 收藏代码
  1. ProgressDialog mypDialog=new ProgressDialog(this);  
  2.   
  3.            //实例化  
  4.   
  5.            mypDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);  
  6.   
  7.            //设置进度条风格,风格为圆形,旋转的  
  8.   
  9.            mypDialog.setTitle("Google");  
  10.   
  11.            //设置ProgressDialog 标题  
  12.   
  13.            mypDialog.setMessage(getResources().getString(R.string.second));  
  14.   
  15.            //设置ProgressDialog 提示信息  
  16.   
  17.            mypDialog.setIcon(R.drawable.android);  
  18.   
  19.            //设置ProgressDialog 标题图标  
  20.   
  21.            mypDialog.setButton("Google",this);  
  22.   
  23.            //设置ProgressDialog 的一个Button  
  24.   
  25.            mypDialog.setIndeterminate(false);  
  26.   
  27.            //设置ProgressDialog 的进度条是否不明确  
  28.   
  29.            mypDialog.setCancelable(true);  
  30.   
  31.            //设置ProgressDialog 是否可以按退回按键取消  
  32.   
  33.            mypDialog.show();  
  34.   
  35.            //让ProgressDialog显示  
 ProgressDialog mypDialog=new ProgressDialog(this);

            //实例化

            mypDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);

            //设置进度条风格,风格为圆形,旋转的

            mypDialog.setTitle("Google");

            //设置ProgressDialog 标题

            mypDialog.setMessage(getResources().getString(R.string.second));

            //设置ProgressDialog 提示信息

            mypDialog.setIcon(R.drawable.android);

            //设置ProgressDialog 标题图标

            mypDialog.setButton("Google",this);

            //设置ProgressDialog 的一个Button

            mypDialog.setIndeterminate(false);

            //设置ProgressDialog 的进度条是否不明确

            mypDialog.setCancelable(true);

            //设置ProgressDialog 是否可以按退回按键取消

            mypDialog.show();

            //让ProgressDialog显示





ProgressDialog中的长形进度条
     
代码实现:
Java代码 复制代码 收藏代码
  1. ProgressDialog mypDialog=new ProgressDialog(this);  
  2.   
  3. //实例化  
  4.   
  5.             mypDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
  6.   
  7.             //设置进度条风格,风格为长形,有刻度的  
  8.   
  9.             mypDialog.setTitle("地狱怒兽");  
  10.   
  11.             //设置ProgressDialog 标题  
  12.   
  13.             mypDialog.setMessage(getResources().getString(R.string.second));  
  14.   
  15.             //设置ProgressDialog 提示信息  
  16.   
  17.             mypDialog.setIcon(R.drawable.android);  
  18.   
  19.             //设置ProgressDialog 标题图标  
  20.   
  21.             mypDialog.setProgress(59);  
  22.   
  23.             //设置ProgressDialog 进度条进度  
  24.   
  25.             mypDialog.setButton("地狱曙光",this);  
  26.   
  27.             //设置ProgressDialog 的一个Button  
  28.   
  29.             mypDialog.setIndeterminate(false);  
  30.   
  31.             //设置ProgressDialog 的进度条是否不明确  
  32.   
  33.             mypDialog.setCancelable(true);  
  34.   
  35.             //设置ProgressDialog 是否可以按退回按键取消  
  36.   
  37.             mypDialog.show();  
  38.   
  39.             //让ProgressDialog显示      
  40.   
  41.   
  42.   
  43. AlertDialog.Builder  
  44.   
  45. AlertDialog中的圆形ProgressBar  
请注明出处,此文档来自“善思善学”。
原文地址:https://www.cnblogs.com/gtgl/p/3906620.html