HarmonyOS 鸿蒙Next关于Tabs只能居中的问题
HarmonyOS 鸿蒙Next关于Tabs只能居中的问题
Tabs是否支持左对齐,右对齐
2 回复
Tabs组件未提供居左的样式,可以用Flex自定义一个靠左的tabBar组件,在onclick事件中通过修改索引值和Tabs组件的索引联动,实现切换效果
[@Entry](/user/Entry)
[@Component](/user/Component)
struct Index {
private tabsController: TabsController = new TabsController()
[@State](/user/State) currentIndex: number = 0;
@Builder
TabBarBuilder(title: string, targetIndex: number) {
Text(title)
.fontWeight(targetIndex === this.currentIndex ? FontWeight.Bold : FontWeight.Normal)
.margin({ left: 10, right: 10 })
.onClick(() => {
this.tabsController.changeIndex(targetIndex)
})
}
build() {
Row() {
Column() {
// Flex({ direction: FlexDirection.Row }) {
Flex({ direction: FlexDirection.RowReverse }) {
this.TabBarBuilder(‘页签1’, 0)
this.TabBarBuilder(‘页签2’, 1)
this.TabBarBuilder(‘页签3’, 2)
}
Tabs({ barPosition: BarPosition.End, controller: <span class="hljs-keyword">this</span>.tabsController }) {
TabContent() {
Text(<span class="hljs-string">"页签1页面"</span>)
}
TabContent() {
Text(<span class="hljs-string">"页签2页面"</span>)
}
TabContent() {
Text(<span class="hljs-string">"页签3页面"</span>)
}
}.onChange((index: number) => {
<span class="hljs-keyword">this</span>.currentIndex = index;
})
}
}
}
}