Android开发高级组件--ProgressBar(进度条组件)

1、进度条组件ProgressBar是在某些操作的进度发展情况指示器,为用户呈现操作的进度,操作完成后进度条被填满。进度条能够直观的帮助用户了解等待一定时间的操作所需要的时间。

2、其层次结构如下:
   java.lang.Object
      android.view.View
         android.widget.ProgressBar

3、Android系统提供的进度条有对话框进度条、标题进度条和水平进度条。进度条的样式也分为水平进度条和圆形转动进度条。圆形进度条表示一个运转的过程,如发短信、连接网络等,表示一个过程正在进行中,一般在xml中定义就可以了:

  
<ProgressBar
      android:id="@+id/pBar"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center_vertical"/>

   此时没有指定它的风格,默认是圆形的,一直会旋转的进度条,如果希望是超大号圆形进度条,只需设置style属性,即style="?android:attr/progressBarStyleLarge",小号的对应风格为style="?android:attr/progressBarStyleSmall",标题型对应的风格为style="?android:attr/progressBarStyleSmallTitle",水平进度条对应的为style="?android:attr/progressBarStyleHorizontal"。

4、标题栏进度条创建步骤
1、调用Activity程序的requestWindowFeatures()方法获取进度条
例如:requestWindowFeature(Window.FEATURE_PROGRESS);
2、调用Activity程序的setProgressBarIndeterminateVisibility()方法,显示进度条对话框
例如:setProgressBarVisibility(true);
3、设置进度值
例如:setProgress(myProgressBar.getProgress()*100);
setSecondaryProgress(myProgressBar.getSecondaryProgress()*100);

5、水平进度条创建过程
1、在布局文件中声明ProgressBar:

   <ProgressBar
      android:id="@+id/pBar"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center_vertical"
      style="?android:attr/progressBarStyleHorizontal"
      android:max="100"
      android:progress="50"
      android:secondaryProgress="70"/>

2、在Activity中获得ProgressBar实例:
   private ProgressBar myProgressBar;
   myProgressBar = (ProgressBar) findViewById(R.id.pBar);
3、调用Progress的incrementProgressBy()方法增加和减少进度
   myProgressBar.incrementProgressBy(5);
   myProgressBar.incrementSecondaryProgress(5);

6、使用实例:
   新建progressbar_xml.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:text="大号进度条"/>

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        style="?android:attr/progressBarStyleLarge"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:text="小号进度条"/>

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        style="?android:attr/progressBarStyleSmall"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:text="水平进度条"/>

    <ProgressBar
        android:id="@+id/progress_horizontal"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:max="200"
        android:progress="50"
        android:secondaryProgress="75"/>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/add"
            android:text="增加"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/dec"
            android:text="减少"/>

        </LinearLayout>

</LinearLayout>

   新建ProgressBarActivity.java
package xiao.fuyan.testapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;

/**
 * Created by xiao on 2017/1/11.
 */
public class ProgressBarActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.progressbar_xml);
        //创建标题栏进度条
        requestWindowFeature(Window.FEATURE_PROGRESS);
        //设置标题栏进度条可见
        setProgressBarVisibility(true);

        //获取水平进度条
        final ProgressBar progressHorizontal = (ProgressBar) findViewById(R.id.progress_horizontal);
        setProgress(progressHorizontal.getProgress() * 100);
        setSecondaryProgress(progressHorizontal.getSecondaryProgress() * 100);

        Button button = (Button) findViewById(R.id.add);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //每次增加5
                progressHorizontal.incrementProgressBy(5);
                setProgress(progressHorizontal.getProgress() * 100);
            }
        });

        button = (Button) findViewById(R.id.dec);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                progressHorizontal.incrementProgressBy(-5);
                setProgress(progressHorizontal.getProgress() * 100);
            }
        });

    }
}

猜你喜欢

转载自xiaofuyan.iteye.com/blog/2353164