问题现象
应用中有的Tabs页面的数量是通过服务端返回的结果动态显示的,TabBar的数量较多时保持均分显示,但是数量较少时希望能够靠拢显示,避免有大量的留白间距影响视觉体验。
背景知识
Tabs组件:通过页签进行内容视图切换的容器组件,每个页签对应一个内容视图。
barGridAlign:以栅格化方式设置TabBar的可见区域。
可以通过改变入参BarGridColumnOptions动态调整TabBar的显示效果。
解决方案
设置Tabs的barGridAlign属性,通过调整BarGridColumnOptions的sm和margin属性,以达到TabBar靠拢或者均分的效果。
用户在输入框输入小于等于10的数字后,以4为阈值:TabBar数量小于4时,页签靠拢显示;TabBar数量大于等于4时,页签均分显示。
import { promptAction } from '@kit.ArkUI';
@Entry
@Component
struct TabsView {
@State tabList: string[] = [];
tabsController: TabsController = new TabsController();
currentIndex: number = 0;
tabsNum: number = 0;
controller: TextInputController = new TextInputController();
private generateCharacterList(n: number): string[] {
let list: string[] = [];
for (let i = 1; i <= n; i++) {
list.push(i.toString());
}
return list;
}
build() {
Column() {
Row() {
TextInput({ text: ${this.tabsNum}, placeholder: 'input your tab number', controller: this.controller })
.backgroundColor('#ffd9dbd9')
.borderWidth(1)
.type(InputType.Number)
.width('90%')
.onChange((value: string) => {
let tabNum = Number(value);
if (tabNum > 10) {
this.getUIContext().getPromptAction().showToast({
message: 'tab number must be little then 10',
duration: 2000,
showMode: promptAction.ToastShowMode.DEFAULT,
bottom: 80
});
return;
}
this.tabList = this.generateCharacterList(Number(value));
});
};
Tabs({ controller: this.tabsController }) {
ForEach(this.tabList, (item: string) => {
TabContent() {
Column() {
Text(item);
}.width('100%').height('100%')
.justifyContent(FlexAlign.Center);
}.tabBar(item);
}, (item: string) => item);
}
.width('100%')
.height('100%')
.barGridAlign({ sm: 4, margin: this.tabList.length < 4 ? 80 : 0 })
.onChange((index: number) => {
this.currentIndex = index;
});
};
}
}
总结
要实现Tabs组件中的TabBar根据数量均分或者靠拢显示,通过设置Tabs组件的barGridAlign属性调整。
https://pastebin.com/3Ly3bcWy
https://pastebin.com/LYjSACKq
https://pastebin.com/ZxfpDHVj
https://pastebin.com/wRMmchsX
https://pastebin.com/QUGVydeQ
https://pastebin.com/u7t485Cj
https://pastebin.com/MgSpCmgN
https://pastebin.com/jHzQbywP
https://pastebin.com/Y40AYaFR
https://pastebin.com/TqhZnSeW
https://pastebin.com/xsBxKer2
https://pastebin.com/BH3fBhnZ
https://pastebin.com/QFYJx63v
https://pastebin.com/3p4N0Py3
https://pastebin.com/CA0FYaxb
https://pastebin.com/YhUWic00
https://pastebin.com/MWXHM7xV
https://pastebin.com/Z2cuN63Z
https://pastebin.com/TwZfsmhf
https://pastebin.com/fd1jtjkK
https://pastebin.com/YbxCHgmq
https://pastebin.com/bWAB2ikf
https://pastebin.com/zaFuKHz8
https://pastebin.com/p1YPsiAs
https://pastebin.com/C3qmtips
https://pastebin.com/BrKP7G4Z
https://pastebin.com/AF5K2vRX
https://pastebin.com/UBJvJrvn
https://pastebin.com/U5KzLteC
https://pastebin.com/dsKycxPx
https://pastebin.com/r89AVWqA
https://pastebin.com/ZsU2Ue2X
https://pastebin.com/sW8MD89N
https://pastebin.com/gM9cBhuc
https://pastebin.com/cSGbxEua
https://pastebin.com/VTFX61JA
https://pastebin.com/xJM7sWLc
https://pastebin.com/cV7C0pVU
https://pastebin.com/EcpqqfMT
https://pastebin.com/WJwPDqt6
https://pastebin.com/nFRN8EKT
https://pastebin.com/7npyFc3C
https://pastebin.com/TBpD5Mp6
https://pastebin.com/yd9nnFAD
https://pastebin.com/Giwtdqty
https://pastebin.com/u6Q4av99
https://pastebin.com/v0uZaX3U
https://pastebin.com/EMUJpJJf
https://pastebin.com/vF6D5GY5
https://pastebin.com/SVZ9R4YT
https://pastebin.com/yCAm4avD
https://pastebin.com/yUUKNpBL
https://pastebin.com/R5LcLsBz
https://pastebin.com/7mXfFRTR
https://pastebin.com/8wsq4anY
https://pastebin.com/zrMi0XY5
https://pastebin.com/mPWZimeE
https://pastebin.com/wLyNkrak
https://pastebin.com/JxHJpMLn
https://pastebin.com/WBNxS6WM
https://pastebin.com/jRg7TPCV
https://pastebin.com/N5mH2TPA
https://pastebin.com/3uAhHRAB
https://pastebin.com/YVb16Uy6
https://pastebin.com/gayJvvN9
https://pastebin.com/0hNG69qt
https://pastebin.com/nW2jpDJ8
https://pastebin.com/sKj03dvR
https://pastebin.com/NvSxR2ET
https://pastebin.com/S9FcRvU2
https://pastebin.com/jxz50vLG
https://pastebin.com/jLEie1Fv
https://pastebin.com/j9nkXqs6
https://pastebin.com/DRW8PV3e
https://pastebin.com/LEMxccyN
https://pastebin.com/8DFFRXxD
https://pastebin.com/24N27gCf
https://pastebin.com/cMz66Ywu
https://pastebin.com/XHiyFYjc
https://pastebin.com/id7SDE72
https://pastebin.com/XZbXmcFy
https://pastebin.com/nMiK8CPL
https://pastebin.com/CG4sH6sX
https://pastebin.com/xuTbGr3H
https://pastebin.com/H7SxBGra
https://pastebin.com/5pcyZ1v1
https://pastebin.com/w8BkwkQX
https://pastebin.com/GCFgXax6
https://pastebin.com/M6a0kFjj
https://pastebin.com/k9CxQJp7
https://pastebin.com/j9kYithV
https://pastebin.com/Fid5ZMmf
https://pastebin.com/gwEAt7KG
https://pastebin.com/Ujqv9U5S
https://pastebin.com/7haFsG38
https://pastebin.com/h6CkhVrt
https://pastebin.com/i2s7AY9h
https://pastebin.com/qc980Yj6
https://pastebin.com/PWsNyWHV
https://pastebin.com/3hByUp7f
https://pastebin.com/GihMasrs
https://pastebin.com/d0LsrM7q
https://pastebin.com/2dgSMjxV
https://pastebin.com/v3p109QD
https://pastebin.com/6Lcb5q5F
https://pastebin.com/5EWSCJCA
https://pastebin.com/bXQr4dcr
https://pastebin.com/ktyjXJK4
https://pastebin.com/ESHSupmR
https://pastebin.com/BvVP6qLm
https://pastebin.com/EW6Mwt3J
https://pastebin.com/T8Jeibx0
https://pastebin.com/HbxPKfCg
https://pastebin.com/YUMYXgFN
https://pastebin.com/3Wzs71fa
https://pastebin.com/e6ATzVcS
https://pastebin.com/PDNxHyrY
https://pastebin.com/SYMGezGi
https://pastebin.com/qWqeyEQA
https://pastebin.com/Dw53j8fk
https://pastebin.com/dpH0wd4G
https://pastebin.com/h8PV90jS
https://pastebin.com/LythSJ1y
https://pastebin.com/RTy9ayLH
https://pastebin.com/kLSkWsNe
https://pastebin.com/MyDjc2jx
https://pastebin.com/ehPPReBz
https://pastebin.com/r1pdjK7k
https://pastebin.com/6YYCmg8d
https://pastebin.com/VHhrkmL6
https://pastebin.com/5V6Ld6tv
https://pastebin.com/GSEFiNLx
https://pastebin.com/2fFDZiJF
https://pastebin.com/iHeLCfN1
https://pastebin.com/7SQhQpD9
https://pastebin.com/iHYUaq04
https://pastebin.com/W93pBuEu
https://pastebin.com/27bRebCU
0 个回复