HarmonyOS Navigation组件,如何获取分栏与不分栏状态下的页面宽高

HarmonyOS
2024-12-24 18:02:31
1086浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

Navigation目前没有具体的获取宽度的API方法,需要自己计算,比如获取屏幕宽度-按钮(100%)宽度=分栏的宽度

可以参考如下demo

import { display } from '@kit.ArkUI';
@Entry
@Component
struct page240706160736038 {
  private arr: number[] = [1, 2, 3];
  private displayWidth: number = 0
  private displayHeight: number = 0
  private pageWidth: number = 0
  private pageHeight: number = 0
  build() {
    Column() {
      Navigation() {
        List({ space: 12 }) {
          ForEach(this.arr, (item:string) => {
            ListItem() {
              NavRouter() {
                Text("NavRouter" + item)
                  .id("NavRouter" + item)
                  .width("100%")
                  .height(72)
                  .backgroundColor('#FFFFFF')
                  .borderRadius(24)
                  .fontSize(16)
                  .fontWeight(500)
                  .textAlign(TextAlign.Center)
                  .onTouch((event) => {
                    if (event.type == TouchType.Up) {
                      const context = this.getUIContext()
                        .getComponentUtils()
                        .getRectangleById("NavRouter" + item)?.size
                      if (context) {
                        this.pageWidth = context.width;
                        this.pageHeight = context.height;
                      }
                      console.log(`
                               displayWidth = ${this.displayWidth}
                               displayHeight = ${this.displayHeight}
                               pageWidth = ${this.pageWidth}
                               pageHeight = ${this.pageHeight}`)
                    }
                  })
                NavDestination() {
                  Text("NavDestinationContent" + item)
                    .id("NavDestinationContent" + item)
                }
              }
            }
          }, (item:string):string => item)
        }
        //设置的导航按钮的宽度
        .width("80%")
        .margin({ top: 12 })
      }
      .title("主标题")
      .mode(NavigationMode.Split)
      //设置导航栏宽度
      .navBarWidth("80%")
    }
    .height('100%')
    .width('100%')
    .backgroundColor('#F1F3F5')
  }

  aboutToAppear(): void {
    // 屏幕宽度
    this.displayWidth = display.getDefaultDisplaySync().width
    // 屏幕高度
    this.displayHeight = display.getDefaultDisplaySync().height
  }
}
  • 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.
  • 68.
  • 69.
  • 70.
分享
微博
QQ
微信
回复
2024-12-24 20:21:13


相关问题
HarmonyOS 横屏状态下获取组件
1320浏览 • 1回复 待解决
HarmonyOS 关于分栏断点优化问题
615浏览 • 1回复 待解决
请教一关于应用分栏实现
2060浏览 • 1回复 待解决
如何设置组件不同状态下样式
2656浏览 • 1回复 待解决
何设置组件不同状态下样式
2809浏览 • 1回复 待解决
TextInput组件输入状态下隐藏光标
2270浏览 • 1回复 待解决
HarmonyOS 刘海获取不到
550浏览 • 1回复 待解决
ArkTs如何获取组件
5782浏览 • 1回复 待解决
HarmonyOS 如何获取组件大小,宽
1256浏览 • 1回复 待解决
如何选择Navigation 组件 Tabs 组件
3417浏览 • 1回复 待解决
HarmonyOS如何获取指定子组件
2081浏览 • 1回复 待解决