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

HarmonyOS
2024-12-24 18:02:31
浏览
收藏 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
  }
}
分享
微博
QQ
微信
回复
2024-12-24 20:21:13
相关问题
HarmonyOS 横屏状态下获取组件
703浏览 • 1回复 待解决
HarmonyOS 关于分栏断点优化问题
166浏览 • 1回复 待解决
如何设置组件不同状态下样式
2158浏览 • 1回复 待解决
请教一关于应用分栏实现
1333浏览 • 1回复 待解决
何设置组件不同状态下样式
2294浏览 • 1回复 待解决
TextInput组件输入状态下隐藏光标
1643浏览 • 1回复 待解决
HarmonyOS 刘海获取不到
210浏览 • 1回复 待解决
ArkTs如何获取组件
4935浏览 • 1回复 待解决
如何选择Navigation 组件 Tabs 组件
2779浏览 • 1回复 待解决
HarmonyOS如何获取指定子组件
1471浏览 • 1回复 待解决