HarmonyOS 修改状态栏颜色不生效

HarmonyOS 修改状态栏颜色不生效。

HarmonyOS
2024-10-10 11:59:52
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Heiang

修改状态栏颜色可以参考如下代码:

import window from '@ohos.window';  
import router from '@ohos.router';  
@Entry  
@Component  
export struct CommonTopBar {  
  @Prop title: string  
  @Prop alpha: number = 1  
  @State statusBarHeight: number = 0  
  private titleAlignment: TextAlign = TextAlign.Center  
  private backButton: boolean = true  
  private onBackClick?: () => void  
  @State barHeight: number = 0  
  aboutToAppear() {  
    // this.setSystemStatusBar()  
    this.setSystemBar()  
  }  
  async setSystemBar() {  
    // 获取当前应用窗口  
    let windowClass: window.Window = await window.getLastWindow(getContext(this))  
    // 获取状态栏高度  
    this.barHeight = windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM).topRect.height  
    await windowClass.setWindowSystemBarProperties({  
      // 颜色属性为ARGB,将蒙尘设置为0%使其透明  
      // 导航栏颜色  
      navigationBarColor:  
      '#fd121de5',  
      // 状态栏颜色  
      statusBarColor: '#ff0ad9c2',  
      // 导航栏文字颜色  
      navigationBarContentColor: '#fde20606',  
      // 状态栏文字颜色  
      statusBarContentColor: '#fff1e50a',  
    })  
  }  
  build() {  
    Column() {  
      Blank()  
        .backgroundColor(Color.Red)  
        .opacity(this.alpha)  
      Stack({ alignContent: Alignment.Start }) {  
        Stack()  
          .height(50)  
          .width("100%")  
          .opacity(this.alpha)  
          .backgroundColor(Color.Red)  
        Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center }) {  
          Text(this.title)  
            .flexGrow(1)  
            .textAlign(this.titleAlignment)  
            .fontColor('#ffffff')  
            .fontSize(16)  
            .align(Alignment.Center)  
            .maxLines(1)  
            .textOverflow({ overflow: TextOverflow.Ellipsis })  
        }  
        .height(50)  
        .margin({ left: 50, right: 50 })  
        .alignSelf(ItemAlign.Center)  
  
        if (this.backButton) {  
          Stack() {  
            Image($r('app.media.icon'))  
              .height(16)  
              .width(16)  
              .align(Alignment.Center)  
              .onClick(() => {  
                this.onBackClick?.()  
                router.back();  
              })  
          }  
          .height(50)  
          .width(50)  
        }  
      }  
      .height(50)  
      .width("100%")  
    }.height(this.statusBarHeight)  
  }  
}
  • 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.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
分享
微博
QQ
微信
回复
2024-10-10 18:16:50


相关问题
HarmonyOS 状态栏颜色如何修改
1110浏览 • 1回复 待解决
鸿蒙怎么 修改状态栏字体颜色
13190浏览 • 1回复 待解决
HarmonyOS 动态设置状态栏颜色
717浏览 • 1回复 待解决
HarmonyOS 状态栏怎么设置颜色
701浏览 • 1回复 待解决
HarmonyOS 状态栏图标颜色设置
799浏览 • 1回复 待解决
HarmonyOS 如何设置状态栏颜色
848浏览 • 1回复 待解决
如何设置状态栏和导航颜色
4179浏览 • 1回复 待解决