[Android]ProgressBar进度条

落日映苍穹つ 2024-03-27 09:14 181阅读 0赞

ProgressBar


ProgressBar是进度条控件,ProgressBar的应用场景很多,比如用户登录时,后台发送请求,以及进行等待服务器返回信息等一些比较耗时的操作。这个时候如果没有提示,用户可能会以为程序崩溃了或手机死机了,会大大降低用户体验,所有在需要进行耗时操作的地方,添加上进度条,让用户知道当前的程序正在执行,也可以直观地告诉用户当前任务的执行进度。

ProgressBar控件的使用


在布局文件中添加控件

  1. <ProgressBar
  2. android:id="@+id/progressBar"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"/>

我们可以通过设置其style属性更改ProgressBar控件的样式(如条形,环形)

ProgressBar控件的属性


ProgressBar控件在使用过程中,经常会使用到以下属性
































属性名称 描述
max 进度条最大值
progress 进度条已完成进度值
indeterminate 如果设置成true,则进度条不精确显示进度
indeterminateDrawable 如果不显示进度的进度条的Drawable对象
indeterminateDuration 设置不精确显示进度的持续条件
progressDrawable 设置轨道对应的Drawable对象

系统提供的进度条


be023911b0ef4f329438ab93e83c03eb.png

  1. <!--系统提供的圆形进度条,依次是小,中,大 -->
  2. <ProgressBar
  3. android:id="@+id/progressBar"
  4. android:layout_width="wrap_content"
  5. android:layout_height="wrap_content"
  6. style="?android:attr/progressBarStyleSmall"/>
  7. <ProgressBar
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. />
  11. <ProgressBar
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. style="?android:attr/progressBarStyleLarge"/>
  15. <!--系统提供的水平进度条-->
  16. <ProgressBar
  17. style="@style/Widget.AppCompat.ProgressBar.Horizontal"
  18. android:progress="18"
  19. android:layout_width="match_parent"
  20. android:layout_height="wrap_content"
  21. />
  22. <ProgressBar
  23. style="@style/Widget.AppCompat.ProgressBar.Horizontal"
  24. android:layout_width="match_parent"
  25. android:layout_height="wrap_content"
  26. android:indeterminate="true"
  27. />

ProgressBar控件的visibility属性使用


在使用ProgressBar控件时,我们经常要控制它的消失和显示,这时可以设置visibility属性。visibility属性的值如下:

visible:表示控件可见。

invisible:表示控件不可见,但会占用原来的位置和大小。

gone:表示控件不可见,但不会占用原来的位置和大小。

  1. val progressBar:ProgressBar=findViewById(R.id.progressBar)
  2. if(progressBar.visibility== View.VISIBLE){
  3. //设置为可见的状态
  4. progressBar.visibility=View.GONE
  5. }else{
  6. //设置为不可见的状态,并且不占用任何空间位置
  7. progressBar.visibility=View.VISIBLE
  8. }

ProgressBar自定义菊花加载


34623189a65a48eaae7a2f9072c7339b.png

  1. <ProgressBar
  2. android:id="@+id/progressBar"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:indeterminateDrawable="@drawable/rotate"
  6. />
  7. <?xml version="1.0" encoding="utf-8"?>
  8. <animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
  9. android:drawable="@drawable/pg"
  10. android:pivotX="50%"
  11. android:pivotY="50%"
  12. android:fromDegrees="0"
  13. android:toDegrees="360">
  14. </animated-rotate>

发表评论

表情:
评论列表 (有 0 条评论,181人围观)

还没有评论,来说两句吧...

相关阅读