HarmonyOS鸿蒙Next中tab导航栏如何动态根据接口均分宽度撑满100%

发布于 1周前 作者 yuanlaile 来自 鸿蒙OS

HarmonyOS鸿蒙Next中tab导航栏如何动态根据接口均分宽度撑满100% tab导航栏如何动态根据接口均分宽度撑满100%

5 回复
你好,可以设置BarMode.Fixed则均分宽度并撑满

可参考如下demo

```javascript
// xxx.ets
[@Entry](/user/Entry)
[@Component](/user/Component)
struct TabsExample {
  [@State](/user/State) scrollMargin: number = 0
  [@State](/user/State) layoutStyle: LayoutStyle = LayoutStyle.ALWAYS_AVERAGE_SPLIT
  [@State](/user/State) fontColor: string = '#182431'
  [@State](/user/State) selectedFontColor: string = '#007DFF'
  [@State](/user/State) currentIndex: number = 0
  [@State](/user/State) controlNums: boolean = true
  private controller: TabsController = new TabsController()
  [@Builder](/user/Builder) TabBuilder(index: number, name: string) {
    Column() {
      Text(name)
        .fontColor(this.currentIndex === index ? this.selectedFontColor : this.fontColor)
        .fontSize(16)
        .fontWeight(this.currentIndex === index ? 500 : 400)
        .lineHeight(22)
    }.width(100)
  }
  build() {
    Column() {
      Tabs({ barPosition: BarPosition.Start, controller: this.controller }) {

        TabContent() {
          Column().width('100%').height('100%').backgroundColor('#007DFF')
        }.tabBar(this.TabBuilder(1, '游戏'))
        TabContent() {
          Column().width('100%').height('100%').backgroundColor('#E67C92')
        }.tabBar(this.TabBuilder(8, '科技'))
        if(this.controlNums) {
          TabContent() {
            Column().width('100%').height('100%').backgroundColor('#FFBF00')
          }.tabBar(this.TabBuilder(2, '数码'))
          TabContent() {
            Column().width('100%').height('100%').backgroundColor('#E67C92')
          }.tabBar(this.TabBuilder(3, '科技'))
          TabContent() {
            Column().width('100%').height('100%').backgroundColor('#00CB87')
          }.tabBar(this.TabBuilder(4, '体育'))
          TabContent() {
            Column().width('100%').height('100%').backgroundColor('#007DFF')
          }.tabBar(this.TabBuilder(5, '影视'))
          TabContent() {
            Column().width('100%').height('100%').backgroundColor('#E67C92')
          }.tabBar(this.TabBuilder(6, '科技'))
        }
      }
      .vertical(false)
      .barMode(BarMode.Fixed, { margin: this.scrollMargin, nonScrollableLayoutStyle: this.layoutStyle })
      .barWidth(360)
      .barHeight(56)
      .animationDuration(400)
      .onChange((index: number) => {
        this.currentIndex = index
      })
      .width(360)
      .height(296)
      .margin({ top: 52 })
      .backgroundColor('#F1F3F5')
    }.width('100%')
  }
}

更多关于HarmonyOS鸿蒙Next中tab导航栏如何动态根据接口均分宽度撑满100%的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


好的,谢谢,

基本信息
姓名:张三
职位:软件工程师
技能:Python, Java, C++

个人简介
我是一名有五年经验的软件工程师,擅长使用Python, Java和C++进行开发。

感谢我设置.barWidth(‘100%’)解决了,

在HarmonyOS鸿蒙Next中,可以通过使用TabList组件来实现动态根据接口均分宽度撑满100%的效果。首先,需要在布局文件中定义TabList组件,并设置其宽度为match_parent。接着,在代码中通过接口获取数据的数量,并根据数量动态设置Tab的宽度。

具体实现步骤如下:

  1. 在布局文件中定义TabList组件:
<TabList
    ohos:id="$+id:tab_list"
    ohos:width="match_parent"
    ohos:height="wrap_content"
    ohos:margin="16vp"
    ohos:orientation="horizontal"/>
  1. 在代码中动态设置Tab的宽度:
TabList tabList = (TabList) findComponentById(ResourceTable.Id_tab_list);
int tabCount = getDataCountFromApi(); // 从接口获取数据数量
for (int i = 0; i < tabCount; i++) {
    Tab tab = new Tab(getContext());
    tab.setWidth(ComponentContainer.LayoutConfig.MATCH_PARENT / tabCount);
    tab.setLayoutConfig(new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT / tabCount, ComponentContainer.LayoutConfig.MATCH_CONTENT));
    tabList.addTab(tab);
}

通过上述步骤,TabList中的Tab会根据接口返回的数据数量动态均分宽度,并撑满整个TabList的宽度。

在HarmonyOS鸿蒙Next中,可以通过TabList组件实现动态均分宽度的导航栏。首先,使用TabListTab组件创建导航栏,然后通过Flex布局设置justifyContentFlexAlign.SpaceBetween,使Tab均匀分布。最后,通过接口获取数据后,动态设置Tab的宽度为100% / tab数量,确保撑满整个宽度。

回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!