Android development advanced components--ProgressBar (progress bar component)

1. The progress bar component ProgressBar is an indicator of the progress of certain operations, showing the progress of the operation to the user. After the operation is completed, the progress bar is filled. The progress bar can intuitively help users understand the time required for an operation that waits for a certain period of time.

2. Its hierarchical structure is as follows:
   java.lang.Object
      android.view.View
         android.widget.ProgressBar

3. The progress bars provided by the Android system include dialog progress bars, title progress bars and horizontal progress bars. The style of the progress bar is also divided into a horizontal progress bar and a circular rotating progress bar. The circular progress bar indicates a running process, such as sending text messages, connecting to the network, etc., indicating that a process is in progress, generally it can be defined in xml:

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

   At this time, its style is not specified. The default is a circular progress bar that will always rotate. If you want an oversized circular progress bar, you only need to set the style attribute, that is, style="?android:attr/progressBarStyleLarge", small The corresponding style of the number is style="?android:attr/progressBarStyleSmall", the style corresponding to the title type is style="?android:attr/progressBarStyleSmallTitle", and the corresponding style of the horizontal progress bar is style="?android:attr/progressBarStyleHorizontal".

4. Steps for creating the title bar progress bar
1. Call the requestWindowFeatures() method of the Activity program to obtain the progress bar
For example : requestWindowFeature(Window.FEATURE_PROGRESS);
2. Call the setProgressBarIndeterminateVisibility() method of the Activity program to display the progress bar dialog box
For example : setProgressBarVisibility( true);
3. Set the progress value
For example : setProgress(myProgressBar.getProgress()*100);
setSecondaryProgress(myProgressBar.getSecondaryProgress()*100);

5. The horizontal progress bar creation process
1. Declare the ProgressBar in the layout file:

   <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. Obtain a ProgressBar instance in Activity:
   private ProgressBar myProgressBar;
   myProgressBar = (ProgressBar) findViewById(R.id.pBar);
3. Call Progress's incrementProgressBy() method to increase and decrease progress
   myProgressBar.incrementProgressBy(5);
   myProgressBar.incrementSecondaryProgress (5);

6. Example of use:
   Create a new progressbar_xml.xml file:

<?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);
            }
        });

    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326357640&siteId=291194637