HarmonyOS 鸿蒙Next Tabs组件的tabBar为什么只能居中显示
HarmonyOS 鸿蒙Next Tabs组件的tabBar为什么只能居中显示
想让tab1 tab2 tab3三个tabBar总宽度占屏幕宽度的50%,整体居左整体居左显示。tabContent的宽度为100%。
试过了所有的sdk10的所有api,都不行,有啥办法么
期望的效果:
目前能实现的效果:
关于HarmonyOS 鸿蒙Next Tabs组件的tabBar为什么只能居中显示的问题,您也可以访问:https://www.itying.com/category-93-b0.html 联系官网客服。
这个很简单啊:其实你在外层包一个Flex组件就好了。
代码:
Flex({ justifyContent: FlexAlign.Start, wrap: FlexWrap.Wrap }){
Tabs({ barPosition: BarPosition.Start }) {
TabContent() {}.tabBar('首页').width('100%')
TabContent() {}.tabBar('推荐').width('100%')
TabContent() {}.tabBar('发现').width('100%')
}.width('50%')
}
效果图:
这个tabContent()里面也变成50%了
果然简单粗暴:)
对的 TabContent就展示不全了
试过吗? 我试了,好像不行,只能居中、均分,不能左对齐。
6楼就是解决方案,吸顶可以用list包裹,tabbar放在listItemGroup的header里,tab放在listItem里,通过list的吸顶属性就可以解决
试下这个第三方
https://ohpm.openharmony.cn/#/cn/detail/[@abner](/user/abner)%2Ftab
一种可以取巧的方式是定义六个tab,后三个进行隐藏处理:
1、页签用空字符串;
2、空白页签的点击响应切换到 N-3 页签,见 3;
3、Tabs的 onChange() 事件中对索引进行 index % 3 操作后调用 controller.changeIndex(idx) 切换回前3个页签,可绕开滑动换页。
总之,把后三个当成前三个的影子;其它是否有影响未知,也未真实测试,只是思路供借鉴。
自定义Tabs状态栏组件
11楼看看,解决了。
你这tabs的50%也不对啊,TabContent只显示了一半
[@Entry](/user/Entry)
[@Component](/user/Component)
struct Application_7 {
[@State](/user/State) currentIndex: number = 0
controller = new TabsController()
@Builder tab(index: number, name: string) {
Row() {
Text(name)
.fontColor(this.currentIndex === index ? Color.Green : Color.Black)
.onClick(() => {
this.controller.changeIndex(index)
})
}.width(50).height(‘100%’).backgroundColor(’#c0c0c0’)
}
@State num: number[] = []
aboutToAppear() {
for (let i = 0; i < 100; i++) {
this.num[i] = i
}
}
build() {
Column() {
Row() {
<span class="hljs-keyword"><span class="hljs-keyword">this</span></span>.tab(<span class="hljs-number"><span class="hljs-number">0</span></span>, <span class="hljs-string"><span class="hljs-string">"首页"</span></span>)
<span class="hljs-keyword"><span class="hljs-keyword">this</span></span>.tab(<span class="hljs-number"><span class="hljs-number">1</span></span>, <span class="hljs-string"><span class="hljs-string">"发现"</span></span>)
<span class="hljs-keyword"><span class="hljs-keyword">this</span></span>.tab(<span class="hljs-number"><span class="hljs-number">2</span></span>, <span class="hljs-string"><span class="hljs-string">"我的"</span></span>)
}.height(<span class="hljs-string"><span class="hljs-string">'10%'</span></span>).width(<span class="hljs-string"><span class="hljs-string">'100%'</span></span>).backgroundColor(<span class="hljs-string"><span class="hljs-string">'#6a6da9'</span></span>)
Tabs({ controller: <span class="hljs-keyword"><span class="hljs-keyword">this</span></span>.controller, barPosition: BarPosition.Start }) {
TabContent() {
List() {
ListItem() {
Text(<span class="hljs-keyword"><span class="hljs-keyword">this</span></span>.num.toString()).width(<span class="hljs-number"><span class="hljs-number">50</span></span>).backgroundColor(<span class="hljs-string"><span class="hljs-string">'#c0c0c0'</span></span>)
}
}
}.width(<span class="hljs-string"><span class="hljs-string">'100%'</span></span>).height(<span class="hljs-string"><span class="hljs-string">'100%'</span></span>).backgroundColor(<span class="hljs-string"><span class="hljs-string">'#c0c0c0'</span></span>)
TabContent() {
Column() {
Text(<span class="hljs-string"><span class="hljs-string">'222'</span></span>)
}.width(<span class="hljs-string"><span class="hljs-string">'100%'</span></span>).height(<span class="hljs-string"><span class="hljs-string">'100%'</span></span>).justifyContent(FlexAlign.Start).alignItems(HorizontalAlign.Center)
}.width(<span class="hljs-string"><span class="hljs-string">'100%'</span></span>).height(<span class="hljs-string"><span class="hljs-string">'100%'</span></span>).backgroundColor(<span class="hljs-string"><span class="hljs-string">'#c0c0c0'</span></span>)
TabContent() {
Text(<span class="hljs-string"><span class="hljs-string">'333'</span></span>)
}.width(<span class="hljs-string"><span class="hljs-string">'100%'</span></span>).height(<span class="hljs-string"><span class="hljs-string">'100%'</span></span>).backgroundColor(<span class="hljs-string"><span class="hljs-string">'#c0c0c0'</span></span>)
}
.vertical(<span class="hljs-literal"><span class="hljs-literal">false</span></span>) <span class="hljs-comment"><span class="hljs-comment">// tabBar 放在顶部</span></span>
.barWidth(<span class="hljs-number"><span class="hljs-number">0</span></span>) <span class="hljs-comment"><span class="hljs-comment">// 把tabBar的宽高设置为0,隐藏 tabBar</span></span>
.barHeight(<span class="hljs-number"><span class="hljs-number">0</span></span>)
.width(<span class="hljs-string"><span class="hljs-string">'100%'</span></span>)
.height(<span class="hljs-string"><span class="hljs-string">'90%'</span></span>)
.backgroundColor(<span class="hljs-string"><span class="hljs-string">'#f58220'</span></span>)
.scrollable(<span class="hljs-literal"><span class="hljs-literal">false</span></span>)
.barMode(BarMode.Fixed)
.onChange(index => <span class="hljs-keyword"><span class="hljs-keyword">this</span></span>.currentIndex = index)
}.width(<span class="hljs-string"><span class="hljs-string">'100%'</span></span>).height(<span class="hljs-string"><span class="hljs-string">'100%'</span></span>)
}
}
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>
可以试试这个!我把tabBar隐藏了!原来那个有点小问题,昨天没注意到!
[@Entry](/user/Entry)
[@Component](/user/Component)
struct Application_6 {
@State currentIndex: number = 0
controller = new TabsController()
@Builder tab(index: number, name: string) {
Row() {
Text(name)
.fontColor(this.currentIndex===index?Color.Green:Color.Black)
.onClick(()=>{
this.controller.changeIndex(index)
})
}.width(50).backgroundColor(’#c0c0c0’)
}
build() {
Column(){
Row(){
<span class="hljs-keyword"><span class="hljs-keyword">this</span></span>.tab(<span class="hljs-number"><span class="hljs-number">0</span></span>,<span class="hljs-string"><span class="hljs-string">"首页"</span></span>)
<span class="hljs-keyword"><span class="hljs-keyword">this</span></span>.tab(<span class="hljs-number"><span class="hljs-number">1</span></span>,<span class="hljs-string"><span class="hljs-string">"发现"</span></span>)
<span class="hljs-keyword"><span class="hljs-keyword">this</span></span>.tab(<span class="hljs-number"><span class="hljs-number">2</span></span>,<span class="hljs-string"><span class="hljs-string">"我的"</span></span>)
}.height(<span class="hljs-string"><span class="hljs-string">'10%'</span></span>).width(<span class="hljs-string"><span class="hljs-string">'100%'</span></span>)
Tabs({ controller: <span class="hljs-keyword"><span class="hljs-keyword">this</span></span>.controller, barPosition: BarPosition.Start }) {
TabContent() {
Text(<span class="hljs-string"><span class="hljs-string">'111'</span></span>)
}
TabContent() {
Text(<span class="hljs-string"><span class="hljs-string">'222'</span></span>)
}
TabContent() {
Text(<span class="hljs-string"><span class="hljs-string">'333'</span></span>)
}
}.width(<span class="hljs-string"><span class="hljs-string">'100%'</span></span>)
.height(<span class="hljs-string"><span class="hljs-string">'90%'</span></span>)
.backgroundColor(<span class="hljs-string"><span class="hljs-string">'#f58220'</span></span>)
.scrollable(<span class="hljs-literal"><span class="hljs-literal">false</span></span>)
.barMode(BarMode.Fixed)
.onChange(index=><span class="hljs-keyword"><span class="hljs-keyword">this</span></span>.currentIndex=index)
}.width(<span class="hljs-string"><span class="hljs-string">'100%'</span></span>).height(<span class="hljs-string"><span class="hljs-string">'100%'</span></span>)
}
}
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>
不用tabbar,自定义一个导航栏!(方便能问一下,您现在做的是哪个app呀)
这个方法不错。但是,tabBar自带吸顶功能,如果不用tabBar的话,要自己去实现上下滑动时的吸顶功能,有点麻烦。