HarmonyOS NavDestination的title怎么居中

NavDestination的title的title默认是靠左的,怎么居中。

HarmonyOS
2024-12-20 16:37:08
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

NavDestination 可以自定义title样式参考Demo

@Entry
@Component
struct NavRouterExample {
  @State isActiveWLAN: boolean = false
  @State isActiveBluetooth: boolean = false
  @Builder titleStyle(){
    Text('标题')
      .textAlign(TextAlign.Center)
      .fontSize(30)
      .position({ x: '50%'})
      .translate({x:'-50%'})
  }
  build() {
    Column() {
      Navigation() {
        NavRouter() {
          Row() {
            Row().width(30).height(30).borderRadius(30).margin({ left: 3, right: 10 }).backgroundColor(Color.Pink)
            Text(`WLAN`)
              .fontSize(22)
              .fontWeight(500)
              .textAlign(TextAlign.Center)
          }
          .width('90%')
          .height(72)
          NavDestination() {
            Flex({ direction: FlexDirection.Row }) {
              Text('未找到可用WLAN').fontSize(30).padding({ left: 15 })
            }
          }
          .title(this.titleStyle())
          .hideTitleBar(false).backgroundColor('#0c182431')
        }.backgroundColor(this.isActiveWLAN ? '#ccc' : '#fff')
        .borderRadius(24)
        .onStateChange((isActivated: boolean) => {
          this.isActiveWLAN = isActivated
        })

        NavRouter() {
          Row() {
            Row().width(30).height(30).borderRadius(30).margin({ left: 3, right: 10 }).backgroundColor(Color.Pink)
            Text(`蓝牙`)
              .fontSize(22)
              .fontWeight(500)
              .textAlign(TextAlign.Center)
          }
          .width('90%')
          .height(72)

          NavDestination() {
            Flex({ direction: FlexDirection.Row }) {
              Text('未找到可用蓝牙').fontSize(30).padding({ left: 15 })
            }
          }.hideTitleBar(false).backgroundColor('#0c182431')
        }.backgroundColor(this.isActiveBluetooth ? '#ccc' : '#fff')
        .borderRadius(24)
        .onStateChange((isActivated: boolean) => {
          this.isActiveBluetooth = isActivated
        })
      }
      .title('设置')
      .titleMode(NavigationTitleMode.Free)
      .mode(NavigationMode.Auto)
      .hideTitleBar(false)
      .hideToolBar(true)
    }.height('100%')
  }
}

参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-basic-components-navdestination-V5

分享
微博
QQ
微信
回复
2024-12-20 19:14:49
相关问题
HarmonyOS Navigation中title怎么居中显示
374浏览 • 1回复 待解决
HarmonyOS AlertDialogtitle居中显示吗
297浏览 • 1回复 待解决
HarmonyOS 获取网页Title 应该怎么获取
1248浏览 • 1回复 待解决
HarmonyOS Row组件怎么居中
183浏览 • 1回复 待解决
HarmonyOS NavDestinationmenu问题
145浏览 • 1回复 待解决
HarmonyOS NavDestinationexpandSafeArea问题
287浏览 • 1回复 待解决