Android ProgressBar示例

Welcome to Android ProgressBar Example. Today we’ll implement android ProgressBar in our application. There are two types of progress bars : Horizontal and Circular. We’ll create both of these progress bar in android application.

欢迎使用Android ProgressBar示例。 今天,我们将在应用程序中实现android ProgressBar 。 有两种类型的进度条: 水平圆形 。 我们将在android应用程序中创建这两个进度条。

Android ProgressBar (Android ProgressBar)

Android ProgressBar is a graphical view indicator that shows some progress. Android progress bar displays a bar representing the completing of the task. Progress bar in android is useful since it gives the user an idea of time to finish its task.

Android ProgressBar是一个图形视图指示器,显示了一些进度。 Android进度栏显示一个代表任务完成的栏。 android中的进度条很有用,因为它使用户有时间完成任务。

Using a ProgressBar is a good user experience practice since it displays the status of progress of the given task (such as downloading an image) to the user.

使用ProgressBar是一种很好的用户体验做法,因为它向用户显示给定任务的进度状态(例如下载图像)。

Android ProgressBar属性 (Android ProgressBar attributes)

Some important attributes used to describe a ProgressBar are given below.

下面提供了一些用于描述ProgressBar重要属性。

  1. android:max : We can set the maximum value of the ProgressBar using this attribute. By default the progress bar maximum value is 100

    android:max :我们可以使用此属性设置ProgressBar的最大值。 默认情况下,进度条最大值为100
  2. android:indeterminate : A boolean value is set depending on whether the time is determinate or not. Setting this attribute to false would show the actual progress. Else if it’s set to true a cyclic animation is displayed to show that progress is happening

    android:indeterminate :根据时间是否确定来设置布尔值。 将此属性设置为false将显示实际进度。 否则,如果将其设置为true,则会显示一个循环动画以表明进度正在发生
  3. android:minHeight : It’s used to set the height of the ProgressBar

    android:minHeight :用于设置ProgressBar的高度
  4. android:minWidth : It’s used to set the width of the ProgressBar

    android:minWidth :用于设置ProgressBar的宽度
  5. android:progress : It’s used to set the number by which the progress bar value will be incremented

    android:progress :用于设置进度条值将递增的数字
  6. style : By default the progress bar will be displayed as a spinning wheel. If we want it to be displayed as a horizontal bar, we need to set the attribute as : style=”?android:attr/progressBarStyleHorizontal”

    style :默认情况下,进度条将显示为旋转轮。 如果我们希望将其显示为水平条,则需要将属性设置为:style =”?android:attr / progressBarStyleHorizo​​ntal”

In this tutorial we’ll be creating a ProgressBar and increment its values by updating inside a thread. We’ll make the thread to sleep for 200 milliseconds after incrementing the values to show the progress slowly.

在本教程中,我们将创建一个ProgressBar并通过在线程内部进行更新来增加其值。 递增值以缓慢显示进度后,我们将使线程Hibernate200毫秒。

Android进度栏示例项目结构 (Android Progress Bar Example Project Structure)

This project consists of a single Activity and layout that contains both the type of Progress Bar.

该项目由单个活动和布局组成,同时包含进度条的类型。

Android进度条码 (Android Progress Bar Code)

The activity_main.xml contains a RelativeLayout as the parent view which contains a Horizontal ProgressBar and a Circular one along with a TextView to display the progress in numeric terms.

activity_main.xml包含一个RelativeLayout作为父视图,其中包含一个Horizo​​ntal ProgressBar和一个Circular以及一个TextView以数字形式显示进度。

activity_main.xml

activity_main.xml

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="23dp"
        android:layout_marginTop="20dp"
        android:indeterminate="false"
        android:max="100"
        android:minHeight="50dp"
        android:minWidth="200dp"
        android:progress="1" />

    <ProgressBar
        android:id="@+id/progressBar_cyclic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minHeight="50dp"
        android:minWidth="50dp"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/progressBar"
        android:layout_below="@+id/progressBar"/>

</RelativeLayout>

In the above layout, the horizontal progress bar values are updated by one as set in android:progress. The circular progress bar runs continuously unless the activity stops.

在上述布局中,水平进度条值会按照android:progress设置更新一个。 除非活动停止,否则圆形进度条将连续运行。

package com.journaldev.progressbar;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private ProgressBar progressBar;
    private int progressStatus = 0;
    private TextView textView;
    private Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        textView = (TextView) findViewById(R.id.textView);
        // Start long running operation in a background thread
        new Thread(new Runnable() {
            public void run() {
                while (progressStatus < 100) {
                    progressStatus += 1;
                    // Update the progress bar and display the
                    //current value in the text view
                    handler.post(new Runnable() {
                        public void run() {
                            progressBar.setProgress(progressStatus);
                            textView.setText(progressStatus+"/"+progressBar.getMax());
                        }
                    });
                    try {
                        // Sleep for 200 milliseconds.
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
}

In the above code the progressBar updates after every 200 milliseconds and the progress bar updates are set using setProgress(). A handler is used to run the background thread. The thread runs until the progressStatus value reaches 100.

在上面的代码中,progressBar每200毫秒更新一次,进度条更新使用setProgress()设置。 处理程序用于运行后台线程。 线程运行直到progressStatus值达到100。

The image given below displays the output of a single instance of our application.

下面给出的图像显示了我们应用程序单个实例的输出。

In this tutorial we’ve created a basic android ProgressBar. We have shown how to add a ProgressBar in a ProgressDialog in a later tutorial. You can download the final Android ProgressBar Project from the below link.

在本教程中,我们创建了一个基本的android ProgressBar。 在后面的教程中,我们已经说明了如何在ProgressDialog中添加ProgressBar。 您可以从下面的链接下载最终的Android ProgressBar项目。

Reference: developer.android.com doc

参考: developer.android.com文档

翻译自: https://www.journaldev.com/9629/android-progressbar-example

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值