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

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

HarmonyOS
2024-03-17 15:20:43
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
rhyine
  • 方案一:通过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组件

分享
微博
QQ
微信
回复
2024-03-18 20:58:35
相关问题
如何设置组件透明效果
67浏览 • 1回复 待解决
SideBarContainer如何设置透明度?
569浏览 • 1回复 待解决
如何设置卡片背景为透明
613浏览 • 1回复 待解决
XComponent 怎么设置透明
347浏览 • 1回复 待解决
List组件如何设置两端渐变效果
219浏览 • 1回复 待解决
Js UI 如何设置状态栏背景是透明
1534浏览 • 1回复 待解决
如何将背景颜色设置透明
315浏览 • 1回复 待解决
如何将AbilityUI界面设置透明
243浏览 • 1回复 待解决
openharmony怎么设置背景透明模糊?
4391浏览 • 1回复 待解决
XComponent 怎么设置透明
221浏览 • 1回复 待解决
能否创建透明ability
72浏览 • 1回复 待解决
设置子窗口透明度未生效
44浏览 • 1回复 待解决
如何设置图片高斯模糊效果
604浏览 • 1回复 待解决
Button组件如何设置渐变背景色
400浏览 • 1回复 待解决