HarmonyOS . 沉浸状态栏使用

1,自定义 AppBar 组件

复制代码
@Component
export struct AppBar {
  private title: string | Resource = '';
  private color?: ResourceColor;
  @StorageProp('topRectHeight')
  topRectHeight:number=0



  @Builder
  loadBuilder() {
  }

  @Builder
  tailingBuilder() {
    Shape().width(28)
  }

  @Builder
  titleBuilder(){
    Text(this.title)
      .fontSize(20)
      .fontWeight(FontWeight.Bold)
      .fontColor($r('app.color.start_window_background'))
  }

  @BuilderParam loading: () => void = this.loadBuilder;
  @BuilderParam tailing: () => void = this.tailingBuilder;
  @BuilderParam titleSlot: () => void = this.titleBuilder;



  build() {
    Stack(){
      Row() {
        this.loading()
        this.tailing()
      }
      .backgroundColor(this.color)
      .width('100%')
      .height(56 )
      .padding({ left: 8, right: 8, })
      .justifyContent(FlexAlign.SpaceBetween)
      this.titleSlot()
    }.padding({ top: px2vp(this.topRectHeight)})

  }

}

2,在EntryAbility中进行,窗口全屏处理,并且同时在onWindowStageCreate方法获取状态栏高度

复制代码
  async onWindowStageCreate(windowStage: window.WindowStage): Promise<void> {
    // Main window is created, set main page for this ability
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
    windowStage.loadContent('pages/Index', (err, data) => {
      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. Data: %{public}s', JSON.stringify(data) ?? '');
    });

    let windowClass: window.Window=windowStage.getMainWindowSync()//获取应用主窗口
    //1,设置窗口全屏
    let isLayoutFullScreen=true
    windowClass.setWindowLayoutFullScreen(isLayoutFullScreen).then(()=>{
      console.info('设置窗口布局为全屏模式')
    }).catch((err)=>{
      console.info('设置窗口布局为全屏模式失败。处理步骤导致'+JSON.stringify(err))
    })
    //2,获取布局避让遮挡的区域

    // 以导航条避让为例 ---api 9以上 (TYPE_NAVIGATION_INDICATOR)
    // let type = window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR
    // let avoidArea=windowClass.getAvoidArea(type)
    // let bottomRectHeight=avoidArea.bottomRect.height; // 获取到导航条区域的高度
    // AppStorage.SetOrCreate('bottomRectHeight', bottomRectHeight)


    // 以状态栏避让为例
    let type=window.AvoidAreaType.TYPE_SYSTEM
    let  avoidArea=await windowClass.getAvoidArea(type)
    let bottomRectHeight= avoidArea.topRect.height
    AppStorage.SetOrCreate('topRectHeight',bottomRectHeight)

    //3,注册监听函数,动态获取避让区域数据
    windowClass.on('avoidAreaChange',(data)=>{
      if (data.type === window.AvoidAreaType.TYPE_SYSTEM) {
        let topRectHeight=data.area.topRect.height
        AppStorage.SetOrCreate('topRectHeight',topRectHeight)
      }
      // else if(data.type == window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR){
      //   let bottomRectHeight = data.area.bottomRect.height;
      //   AppStorage.SetOrCreate('bottomRectHeight', bottomRectHeight);
      // }

    })

  }

3,在pages中的使用

复制代码
import PreferencesUtils from '../dbSQL/PreferencesUtils';
import  {AppBar}  from '../pages/AppBar'
import router from '@ohos.router';

@Entry
@Component
struct Index {
  @State message: string = '';

   aboutToAppear(){
     PreferencesUtils.putString('userName','张三')
     PreferencesUtils.putString('age','18')
     PreferencesUtils.putString('sex','男')
   }

  async getAll(){
    this.message=JSON.stringify(await PreferencesUtils.getAll())
    console.log('getAll', this.message)
  }

  @Builder
  title() {
    Text("Preferences的使用").fontSize('18fp').fontWeight(FontWeight.Bold).margin({ left: 12 })
  }

  @Builder
  tailing() {
    Button() {
      Image($r('app.media.icon')).width(26).height(26).fillColor(Color.Black)
    }
    .width(36).height(36)
    .backgroundColor(Color.Transparent)
    .onClick(() => this.toClick())
  }

  toClick() {
    router.pushUrl({
      url:'pages/CustomDialogView'
    })
  }
  @Builder
  loading() {
    Button() {
      Image($r('app.media.more')).width(30).height(30)
    }
    .width(36).height(36)
    .backgroundColor(Color.Transparent)
  }

  build() {
    Column() {
      AppBar({
        color: Color.White,
        tailing: () => {
           this.tailing()
        },
        loading:()=>{
          this.loading()
        },
        titleSlot: this.title
      })

      Text(this.message)
        .fontSize(20)
        .margin({top:30})
        .fontWeight(FontWeight.Bold)

        Column({space:20}){
          Button('getAll').onClick(async ()=>{
            this.getAll()
          })

          Button('put').onClick(async ()=>{//插入数据key相同时,会自动修改替换value值
            PreferencesUtils.putString('userName','李四')
            PreferencesUtils.putString('age','24')
            PreferencesUtils.putString('sex','女')
            this.getAll()
          })


          Button('update').onClick(async ()=>{
            await PreferencesUtils.update('userName','王二麻子')
            await PreferencesUtils.update('age','35')
            await PreferencesUtils.update('sex','男')
            this.getAll()
          })


          Button('delete').onClick(async ()=>{
            await PreferencesUtils.delete('sex')
            this.getAll()
          })

          Button('clear').onClick(async ()=>{
            await PreferencesUtils.clear()
            this.getAll()
          })
        }.margin({top:30})
        .justifyContent(FlexAlign.Center)
    }
    .backgroundColor('#fafafa')
    .width('100%')
  }
}
相关推荐
熊猫钓鱼>_>20 小时前
普通用户视角的端侧智能体验手记【鸿蒙心迹】
开发语言·人工智能·华为·智能手机·性能优化·ai编程·harmonyos
KevinChaos21 小时前
阳台小花园开发手记 05|HarmonyOS 7.0 互动卡片:动效进出衔接
华为·harmonyos
WHMONY1 天前
大一独立开发鸿蒙原生 AI Agent:聊聊我为什么死磕端侧模型与“技术平权”
harmonyos
todoitbo1 天前
把 K-Lite「什么都能播」搬上鸿蒙 PC:影匣播放器的 FFmpeg + libass 原生移植实录
华为·ffmpeg·harmonyos·鸿蒙
威哥爱编程1 天前
模拟面试智能体开发实战 05:给面试官接上题库和检索,题目结构化、大纲可溯源,AI 不再凭印象出题
python·agent·harmonyos
威哥爱编程1 天前
模拟面试智能体开发实战 06:答得好怎么追问、答得错怎么点破,追问引擎让面试官会深挖会纠错
python·agent·harmonyos
c2892480843s1 天前
【鸿蒙心迹】深色模式适配实战:资源限定词、动态取色与一张图两个主题
移动开发·harmonyos·鸿蒙·arkui·深色模式
贾伟康1 天前
HarmonyOS API 26 Navigation 连续栈操作:中间过程合并、最终状态与强制新实例
harmonyos·arkts·navigation·navpathstack·路由治理
贾伟康2 天前
HarmonyOS API 26 Navigation 跨包路由:router_map、HSP 按需加载与失败回落
harmonyos·arkts·模块化·navigation·hsp
KevinChaos2 天前
阳台小花园开发手记 04|HarmonyOS 7.0 互动卡片:倾斜手机时的卡片效果
华为·harmonyos