如何设置Tabs的末尾由透明到不透明的渐变效果?

如何设置Tabs的末尾由透明到不透明的渐变效果

阅读 337
1 个回答

方案一:通过List组件结合linearGradient自定义实现。

@Entry 
@Component 
struct ListExample { 
  @State arr: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9]; 
  private scroller: Scroller = new Scroller(); 
 
  build() { 
    Stack() { 
      List({ space: 10 }) { 
        ForEach(this.arr, (item: string) => { 
          ListItem() { 
            Text("中国潮色") 
              .width(100) 
              .height(64) 
              .fontColor(Color.White) 
              .backgroundColor(Color.Black) 
              .textAlign(TextAlign.Center) 
          } 
        }, (item: string) => item); 
      } 
      .listDirection(Axis.Horizontal) 
      .scrollBar(BarState.Off) 
      .padding({ top: 20, bottom: 20 }) 
      .width("80%") 
      .height("100%") 
 
      Stack() { 
 
      } 
      .linearGradient({ 
        angle: 90, 
        colors: [[0x000000, 0.0], ['rgba(0,0,0,0)', 0.1], ['rgba(0,0,0,0)', 0.9], [0x000000, 1.0]] 
      }) 
      .width("80%") 
      .height("100%") 
      .hitTestBehavior(HitTestMode.None) 
    }.height(100).width('100%').backgroundColor(Color.Black) 
  } 
}

方案二:API10起可以通过Tabs组件fadingEdge属性来实现边缘渐变消失的效果。

@Entry 
@Component 
struct TabsOpaque { 
  @State message: string = 'Hello World'; 
  private controller: TabsController = new TabsController(); 
  private controller1: TabsController = new TabsController(); 
  @State selfFadingFade: boolean = true; 
 
  build() { 
    Column() { 
      Button('子页签设置渐隐').width('100%').margin({ bottom: '12vp' }) 
        .onClick((event?: ClickEvent) => { 
          this.selfFadingFade = true; 
        }) 
      Button('子页签设置不渐隐').width('100%').margin({ bottom: '12vp' }) 
        .onClick((event?: ClickEvent) => { 
          this.selfFadingFade = false; 
        }) 
      Tabs({ barPosition: BarPosition.End, controller: this.controller }) { 
        TabContent() { 
          Column().width('100%').height('100%').backgroundColor(Color.Pink) 
        }.tabBar('pink') 
 
        TabContent() { 
          Column().width('100%').height('100%').backgroundColor(Color.Yellow) 
        }.tabBar('yellow') 
 
        TabContent() { 
          Column().width('100%').height('100%').backgroundColor(Color.Blue) 
        }.tabBar('blue') 
 
        TabContent() { 
          Column().width('100%').height('100%').backgroundColor(Color.Green) 
        }.tabBar('green') 
 
        TabContent() { 
          Column().width('100%').height('100%').backgroundColor(Color.Green) 
        }.tabBar('green') 
 
        TabContent() { 
          Column().width('100%').height('100%').backgroundColor(Color.Green) 
        }.tabBar('green') 
 
        TabContent() { 
          Column().width('100%').height('100%').backgroundColor(Color.Green) 
        }.tabBar('green') 
 
        TabContent() { 
          Column().width('100%').height('100%').backgroundColor(Color.Green) 
        }.tabBar('green') 
      } 
      .vertical(false) 
      .scrollable(true) 
      .barMode(BarMode.Scrollable) 
      .barHeight(80) 
      .animationDuration(400) 
      .onChange((index: number) => { 
        console.info(index.toString()); 
      }) 
      .fadingEdge(this.selfFadingFade) 
      .height('30%') 
      .width('100%') 
 
      Tabs({ barPosition: BarPosition.Start, controller: this.controller1 }) { 
        TabContent() { 
          Column().width('100%').height('100%').backgroundColor(Color.Pink) 
        }.tabBar('pink') 
 
        TabContent() { 
          Column().width('100%').height('100%').backgroundColor(Color.Yellow) 
        }.tabBar('yellow') 
 
        TabContent() { 
          Column().width('100%').height('100%').backgroundColor(Color.Blue) 
        }.tabBar('blue') 
 
        TabContent() { 
          Column().width('100%').height('100%').backgroundColor(Color.Green) 
        }.tabBar('green') 
 
        TabContent() { 
          Column().width('100%').height('100%').backgroundColor(Color.Green) 
        }.tabBar('green') 
 
        TabContent() { 
          Column().width('100%').height('100%').backgroundColor(Color.Green) 
        }.tabBar('green') 
      } 
      .vertical(true) 
      .scrollable(true) 
      .barMode(BarMode.Scrollable) 
      .barHeight(200) 
      .barWidth(80) 
      .animationDuration(400) 
      .onChange((index: number) => { 
        console.info(index.toString()); 
      }) 
      .fadingEdge(this.selfFadingFade) 
      .height('30%') 
      .width('100%') 
    } 
    .padding({ top: '24vp', left: '24vp', right: '24vp' }) 
  } 
}

参考链接:

Tabs组件

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进