Android中ProgressBar的使用

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

ProgressBar的几个常用属性和方法

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

setProgress(int) 设置当前值


  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>  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/text"
        android:textSize="25sp" />

    <EditText
        android:id="@+id/value"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2"
        android:layout_toRightOf="@id/text"
        android:textSize="15sp" />

    <!-- 定义滚动条
        sytle滚动条样式:progressBarStyleHorizontal一个长条形
        max 滚动条最大值
        progress 滚动条当前值
        visibility 是否可见
     -->
    <ProgressBar
        android:id="@+id/firstBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/text"
        android:layout_below="@+id/text"
        android:max="200"
        android:maxHeight="48dp"
        android:minHeight="48dp"
        android:progress="0"
        android:visibility="visible" />

    <TextView
        android:id="@+id/text2"
        android:layout_below="@id/firstBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/text"
        android:textSize="25sp" />

    <EditText
        android:id="@+id/value2"
        android:layout_below="@id/firstBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2"
        android:layout_toRightOf="@id/text"
        android:textSize="15sp" />

    <!-- 定义滚动条
        sytle滚动条样式:progressBarStyleLarge一个大圆形样式
     -->
    <ProgressBar
        android:id="@+id/firstBar2"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/text2"
        android:layout_below="@+id/text2"
        android:max="200"
        android:progress="0"
        android:visibility="visible" />

     <!-- 定义一个标题栏的滚动条
     -->
    <ProgressBar
        android:id="@+id/firstBar3"
        style="?android:attr/progressBarStyleSmallTitle"
        android:layout_width="200dp"
        android:layout_height="wrap_content"/>
</RelativeLayout>

MainActivity:

处理动态加载滚动条

  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.     }  
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //请求设置窗口标题栏滚动条
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.activity_main);
        pb = (ProgressBar)findViewById(R.id.firstBar);
        value = (EditText)findViewById(R.id.value);
        //设置滚动条可见
        setProgressBarIndeterminateVisibility(true);

        //创建一个Handler
        mHandler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                //处理消息
                switch (msg.what) {
                    case MSG:
                        //设置滚动条和text的值
                        pb.setProgress(pro);
                        value.setText(Integer.toString(pro));
                        break;
                    default:
                        break;
                }
            }
        };
        start();
    }

    private void start()
    {
        new Thread(new Runnable() {
            @Override
            public void run() {
                int max = pb.getMax();
                try {
                    //子线程循环间隔消息
                    while (pro < max) {
                        pro += 10;
                        Message msg = new Message();
                        msg.what = MSG;
                        mHandler.sendMessage(msg);
                        Thread.sleep(1000);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

效果图:


转自sujudz的博客:http://blog.csdn.net/sujudz/article/details/9018861

发布了94 篇原创文章 · 获赞 157 · 访问量 57万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览