文档管理中心
我的

如何实现可调高度的下载按钮

问题现象

ProgressButton默认不支持高度调整,如何实现可调高度的下载按钮?

背景知识

Progress:进度条组件,用于显示内容加载或操作处理等进度。

解决方案

  • 方案逻辑:Progress组件可以配置高度,使用Progress的属性可实现与ProgressButton相同的下载效果。
    1. 通过Progress组件的style属性的content参数,实现在Progress组件上显示按钮文字和进度信息。
      收起
      自动换行
      深色代码主题
      复制
      1. Progress({ value: this.progressIndex, total: 100, type: ProgressType.Capsule })
      2. .width(300)
      3. .height(100) // 设置Progress组件的高度
      4. .color(Color.Blue)
      5. .margin({top:300})
      6. .style({
      7. borderWidth: 1,
      8. content: this.textState, // 设置按钮文字
      9. font: { size: 13, style: FontStyle.Normal },
      10. enableScanEffect: false,
      11. showDefaultPercentage: false
      12. })
    2. 给Progress添加点击事件,控制Progress上显示内容的变化。初始时,显示为“下载”;点击后动态显示下载进度,再次点击后显示“继续”,并且暂停下载进度;进度100%后,显示为“完成”。
      收起
      自动换行
      深色代码主题
      复制
      1. .onClick(() => {
      2. this.isRunning = !this.isRunning;
      3. if (this.textState && !this.isRunning && this.progressIndex < 100) {
      4. this.textState = '继续';
      5. }
      6. let timer = setInterval(() => {
      7. if (this.isRunning && this.progressIndex < 100) {
      8. this.progressIndex++;
      9. this.textState = `${this.progressIndex}%`;
      10. if (this.progressIndex === 100) {
      11. this.textState = '已完成';
      12. this.isRunning = false;
      13. }
      14. } else {
      15. clearInterval(timer);
      16. }
      17. }, 20);
      18. })
    3. 设置Progress组件的高度。
      收起
      自动换行
      深色代码主题
      复制
      1. Progress({ value: this.progressIndex, total: 100, type: ProgressType.Capsule })
      2. .width(300)
      3. .height(100) // 设置Progress组件的高度
  • 完整示例参考如下:
    收起
    自动换行
    深色代码主题
    复制
    1. @Entry
    2. @Component
    3. struct PushButton {
    4. @State progressIndex: number = 0;
    5. @State textState: string = '下载';
    6. @State isRunning: boolean = false;
    7. build() {
    8. Column({ space: 20 }) {
    9. Progress({ value: this.progressIndex, total: 100, type: ProgressType.Capsule })
    10. .width(300)
    11. .height(100) // 设置Progress组件的高度
    12. .color(Color.Blue)
    13. .margin({top:300})
    14. .style({
    15. borderWidth: 1,
    16. content: this.textState, // 设置按钮文字
    17. font: { size: 13, style: FontStyle.Normal },
    18. enableScanEffect: false,
    19. showDefaultPercentage: false
    20. })
    21. .onClick(() => {
    22. this.isRunning = !this.isRunning;
    23. if (this.textState && !this.isRunning && this.progressIndex < 100) {
    24. this.textState = '继续';
    25. }
    26. let timer = setInterval(() => {
    27. if (this.isRunning && this.progressIndex < 100) {
    28. this.progressIndex++;
    29. this.textState = `${this.progressIndex}%`;
    30. if (this.progressIndex === 100) {
    31. this.textState = '已完成';
    32. this.isRunning = false;
    33. }
    34. } else {
    35. clearInterval(timer);
    36. }
    37. }, 20);
    38. })
    39. }.alignItems(HorizontalAlign.Center).width('100%').margin({ top: 20 })
    40. }
    41. }