Android基础-ProgressBar(进度条)

ProgressBar的基本属性 

android:style设置进度条的样式  progressBarStyleHorizontal(水平进度条)

android:progress = "" 设置进度

android:max = "" 设置最大值,默认100

android:indeterminate="true" 设置进度条的间隔

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff0000"
    android:orientation="vertical"
    tools:context=".ProgressBarActivity">

    <!--
        进度条:默认样式是转圈。修改样式需要设置风格
        style 设置风格 progressBarStyleHorizontal(水平进度条)
        android:progress = "" 设置进度
        android:max="" 设置最大值, 默认100
        android:indeterminate="true" 设置进度条


        -->
    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ProgressBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="?android:attr/progressBarStyleHorizontal"
        android:progress="30"
        android:max="200"/>

    <ProgressBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="?android:attr/progressBarStyleHorizontal"
        android:indeterminate="true" />

    <ProgressBar
        android:id="@+id/progress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="?android:attr/progressBarStyleHorizontal"
        />

</LinearLayout>

在实际代码中控件进度条的前进 

这里使用Thread来设置带延时的进度刷新

package com.example.uidemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ProgressBar;

public class ProgressBarActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_progress_bar);

        final ProgressBar progressBar = findViewById(R.id.progress);
//        progressBar.setProgress(80);
        //在android中, 4.0以后不能直接在线程中操作控件
        new Thread() {
            @Override
            public void run() {
                for (int i = 1; i <= 100; i++) {
                    progressBar.setProgress(i);
                    try {
                        Thread.sleep(30);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }

}
原文地址:https://www.cnblogs.com/my-love-is-python/p/14521566.html