Android 基础开发组件ProgressBar

ProgressBar

进度条

<ProgressBar
  android:id="@+id/pb1"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"/>

style

进度条样式
水平进度条

<ProgressBar
  style="?android:attr/progressBarStyleHorizontal"/>

max

最大值

progress

进度条已完成进度

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

indeterminate

如果设置成true则不显示进度

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

progressbar.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".Progressbar">
  <LinearLayout
    android:orientation="vertical"
    android:layout_width="0dp"
    android:layout_height="0dp" app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent">
    <ProgressBar
      android:id="@+id/pb1"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>
    <Button
      android:id="@+id/btn1"
      android:text="显示隐藏进度条"
      android:onClick="onClick"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>
    <ProgressBar
      style="?android:attr/progressBarStyleHorizontal"
      android:id="@+id/pb2"
      android:max="100"
      android:progress="0"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>
    <Button
      android:id="@+id/btn2"
      android:text="模拟下载"
      android:onClick="load"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>
    <ProgressBar
      style="?android:attr/progressBarStyleHorizontal"
      android:id="@+id/pb3"
      android:indeterminate="true"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>
    <Button
      android:id="@+id/btn3"
      android:text="不显示进度"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>
  </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Progressbar.java

package com.weijun901.app;

import android.view.View;
import android.widget.ProgressBar;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class Progressbar extends AppCompatActivity {
  private ProgressBar progressbar1;
  private ProgressBar progressbar2;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.progressbar);
    //通过id找到进度条
    progressbar1 = findViewById(R.id.pb1);
    progressbar2 = findViewById(R.id.pb2);

  }
  //当点击按钮显示进度条,再点击隐藏进度条
  public void onClick(View view){
    if(progressbar1.getVisibility() == View.GONE) {
      progressbar1.setVisibility(View.VISIBLE);
    } else {
      progressbar1.setVisibility(View.GONE);
    }
  }
  //进度条进度默认是0,每点击按钮一次+10,当大于101,回到0
  public void load(View view) {
    int progress = progressbar2.getProgress();
    progress += 10;
    progressbar2.setProgress(progress);
    if (progress > 101){
      progress = 0;
      progressbar2.setProgress(progress);
    }
  }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

望天吼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值