HarmonyOS Navigation无法正确显示

视图层为Tabs-TabContent-Navigation。Tabs里循环创建5个TabContent,然后每个TabContent里就是一个导航控制器Navigation,但是导航栏掉下来了一半。

HarmonyOS
2024-12-24 17:39:36
1.0w浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
shlp

参考示例:

EntryAbility.ets:

import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { window } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';

export default class EntryAbility extends UIAbility {
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
  }

  onDestroy(): void {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
  }

  onWindowStageCreate(windowStage: window.WindowStage): void {
    // Main window is created, set main page for this ability
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');

    windowStage.loadContent('pages/Index', (err) => {
      if (err.code) {
        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
        return;
      }
      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.');
    });

    let windowClass: window.Window = windowStage.getMainWindowSync();
    // 1. 设置窗口全屏
    let isLayoutFullScreen = true;
    windowClass.setWindowLayoutFullScreen(isLayoutFullScreen)
      .then(() => {
        console.info('Succeeded in setting the window layout to full-screen mode.');
      })
      .catch((err: BusinessError) => {
        console.error('Failed to set the window layout to full-screen mode. Cause:' + JSON.stringify(err));
      });
  }
  onWindowStageDestroy(): void {
    // Main window is destroyed, release UI related resources
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
  }
  onForeground(): void {
    // Ability has brought to foreground
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
  }
  onBackground(): void {
    // Ability has back to background
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.

Index.ets:

@Entry
@Component
struct Index {
  private tabBarItemList: string[] = ["首页", "门店", "工单", "资产", "我的"]
  @State private currentIndex: number = 0;
  @Builder
  private buildNavigationTitle(item: string) {
    Column() {
      Text(item)
        .fontSize(16)
        .fontWeight(FontWeight.Regular)
        .fontColor(Color.Red)
    }
    .height("100%")
    .width("100%")
    .backgroundColor(Color.Orange)
    .align(Alignment.Center)
    .justifyContent(FlexAlign.Center)
  }

  @Builder
  private buildTabBarItem(item: string, index: number) {
    Text(item)
      .fontSize(12)
      .fontWeight(FontWeight.Regular)
      .fontColor(index === this.currentIndex ? "#0A59F7" : "#7F7F7F")
  }

  build() {
    Tabs({
      barPosition: BarPosition.End,
      index: this.currentIndex
    }) {
      ForEach(
        this.tabBarItemList,
        (item: string, index: number) => {
          TabContent() {
            Navigation() {
              Column() {
                Text(item).fontSize(30)
              }
              .height("100%")
              .width("100%")
              .backgroundColor(Color.Green)
              .justifyContent(FlexAlign.Start)
            }
            .title(this.buildNavigationTitle(item))
            .backgroundColor(Color.Red)
          }
          .width("100%")
          .height("100%")
          .backgroundColor(Color.Yellow)
          .tabBar(this.buildTabBarItem(item, index))
        }
      )
    }
    .onChange((index: number) => {
      this.currentIndex = index;
    })
    .scrollable(false)
    .animationDuration(0)
    .barBackgroundColor("#F1F3F5")
    .divider({
      strokeWidth: 0.5,
    })
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.

需要给给navigation设置:

Navigation() {}
.titleMode(NavigationTitleMode.Mini)
.hideBackButton(true)
  • 1.
  • 2.
  • 3.
分享
微博
QQ
微信
回复
2024-12-24 18:40:10


相关问题
HarmonyOS 无法正确生成
440浏览 • 1回复 待解决
HarmonyOS Repeat无法正确刷新
315浏览 • 1回复 待解决
HarmonyOS 平板无法正确横竖屏切换
578浏览 • 1回复 待解决
HarmonyOSNavigation显示dialog问题
1084浏览 • 1回复 待解决
HarmonyOS Navigation中title怎么居中显示
485浏览 • 1回复 待解决
HarmonyOS heif图片无法显示
481浏览 • 1回复 待解决
Fluttertoast无法HarmonyOS显示
774浏览 • 2回复 待解决
HarmonyOS px2vp在模拟器上无法正确转换
985浏览 • 1回复 待解决
HarmonyOS 无法显示自己的位置图标
394浏览 • 1回复 待解决
HarmonyOS 跨module调用组件,无法显示
840浏览 • 1回复 待解决
HarmonyOS webview加载页面无法显示
1423浏览 • 1回复 待解决
HarmonyOS TextArea显示无法自动聚焦
381浏览 • 1回复 待解决
HarmonyOS webview部分网页无法显示
671浏览 • 1回复 待解决