当前位置:   article > 正文

Android开发-Android常用组件-ProgressBar进度条

android progressbar进度样式

4.8  ProgressBar进度条

  • 常用属性

android:max

进度条的最大值

android:progress

进度条已完成进度值

android:progressDrawable

设置轨道对应的Drawable对象

android:indeterminate

如果设置成true,则进度条不精确显示进度

android:indeterminateDrawable

设置不显示进度的进度条的Drawable对象

android:indeterminateDuration

设置不精确显示进度的持续时间

android:secondaryProgress

二级进度条,类似于视频播放的一条是当前播放进度,一条是  缓冲进度,前者通过progress属性进行设置

  • 对应的再Java中我们可调用下述方法:

getMax()

返回这个进度条的范围的上限

getProgress()

返回进度

getSecondaryProgress()

返回次要进度

incrementProgressBy(int diff)

指定增加的进度

isIndeterminate()

指示进度条是否在不确定模式下

setIndeterminate(Boolean indeterminate)

设置不确定模式下

  • 设置ProgressBar的样式,不同的样式会有不同的形状和模式:

Widget.ProgressBar.Horizontal

横向进度条(精确模式或模糊模式,这取决于Android:indeterminate)

Widget.ProgressBar

中号的圆形进度条(模糊模式)

Widget.ProgressBar.Small

小号的圆形进度条(模糊模式)

Widget.ProgressBar.Large

大号的圆形进度条(模糊模式)

Widget.ProgressBar.Inverse

中号的圆形进度条(模糊模式),该样式适用于亮色背景(例如白色)

Widget.ProgressBar.Small.Inverse

小号的圆形进度条(模糊模式),该样式适用于亮色背景(例如白色)

Widget.ProgressBar.Large.Inverse

大号的圆形进度条(模糊模式),该样式适用于亮色背景(例如白色)

progress_bar.xml:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical"
  7. tools:context=".MainActivity">
  8. <!-- 系统提供的圆形进度条,fenw小、中、大-->
  9. <ProgressBar
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. style="@android:style/Widget.ProgressBar.Small"/>
  13. <ProgressBar
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"/>
  16. <ProgressBar
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. style="@android:style/Widget.ProgressBar.Large"/>
  20. <!-- 系统提供的水平进度条,分为模糊和不模糊-->
  21. <ProgressBar
  22. android:id="@+id/pb"
  23. android:layout_width="match_parent"
  24. android:layout_height="wrap_content"
  25. style="@android:style/Widget.ProgressBar.Horizontal"
  26. android:max="100"
  27. android:progress="18"/>
  28. <ProgressBar
  29. android:layout_width="match_parent"
  30. android:layout_height="wrap_content"
  31. android:layout_marginTop="10dp"
  32. android:indeterminate="true"  //不精确显示进度
  33. style="@android:style/Widget.ProgressBar.Horizontal"/>
  34. </LinearLayout>

 

  •  模拟进度条更新:
ProgressBarActivity .java:
  1. package com.example.myapplication;
  2. import android.os.Bundle;
  3. import android.os.Handler;
  4. import android.os.Message;
  5. import android.widget.ProgressBar;
  6. import androidx.annotation.NonNull;
  7. import androidx.annotation.Nullable;
  8. import androidx.appcompat.app.AppCompatActivity;
  9. public class ProgressBarActivity extends AppCompatActivity {
  10. private int currentProgress = 0;//当前进度值
  11. private ProgressBar progressBar;
  12. private int maxProgress; //最大进度值
  13. private Handler mHandler = new Handler(){
  14. @Override
  15. public void handleMessage(@NonNull Message msg){
  16. super.handleMessage(msg);
  17. switch (msg.what){
  18. case 0:
  19. progressBar.setProgress(currentProgress);
  20. break;
  21. }
  22. }
  23. };
  24. @Override
  25. protected void onCreate(@Nullable Bundle savedInstanceState){
  26. super.onCreate(savedInstanceState);
  27. setContentView(R.layout.progress_bar);
  28. progressBar = findViewById(R.id.pb);
  29. maxProgress = progressBar.getMax();
  30. }
  31. @Override
  32. protected void onStart(){
  33. super.onStart();
  34. //启动线程模拟加载
  35. new Thread(){
  36. @Override
  37. public void run() {
  38. while (true) {
  39. try {
  40. for (int i = 0; i < 100; i++) {
  41. Thread.sleep(1000);
  42. currentProgress += 10;
  43. if (currentProgress > maxProgress) {
  44. break;
  45. }
  46. mHandler.sendEmptyMessage(0);
  47. }
  48. } catch (InterruptedException e) {
  49. e.printStackTrace();
  50. }
  51. }
  52. }
  53. }.start();
  54. }
  55. }

 启动测试:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/474090
推荐阅读
相关标签
  

闽ICP备14008679号