【每日学点HarmonyOS Next知识】状态栏背景色、全局页面置灰、点击空白关闭弹窗、UI组件作为参数、关闭默认启动动效_harmony next 自定义导航栏控件 关闭页面
1、HarmonyOS 如何获取状态栏的背景色和字体颜色呢?
如何获取状态栏的背景色和字体颜色呢?
参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-window-V5#setwindowsystembarproperties9setWindowSystemBarProperties(systemBarProperties: SystemBarProperties): Promise
设置主窗口三键导航栏、状态栏的属性,使用Promise异步回调,
该接口在2in1设备上调用不生效,其他设备在分屏模式(即窗口模式为window.WindowStatusType.SPLIT_SCREEN)、自由悬浮窗口模式(即窗口模式为window.WindowStatusType.FLOATING)、自由多窗模式(可点击设备控制中心中的自由多窗按钮开启)下调用不会立刻生效,只有进入全屏主窗口才会生效。
setWindowLayoutFullScreen 设置全屏导航栏颜色随页面颜色变化
import { router, window } from \'@kit.ArkUI\';import { common } from \'@kit.AbilityKit\';@Entry@Componentstruct SystemBarPropertiesTest { @State message: string = \'Hello World\'; @State barHeight: number = 0; private context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; async setSystemBar() { // 获取当前应用窗口 let windowClass: window.Window = await window.getLastWindow(this.context) // 获取状态栏高度 this.barHeight = windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM).topRect.height // 前提:设置窗口为全屏窗口 windowClass.setWindowLayoutFullScreen(true); // 沉浸式方案一:设置系统栏不显示[],需要显示则设置[‘status’,‘navigation’] // windowClass.setWindowSystemBarEnable([]); // 沉浸式方案二:将状态栏和导航栏的背景色设置为跟应用窗口相同的颜色 /*await windowClass.setWindowSystemBarProperties({ // 颜色属性为ARGB,将蒙尘设置为0%使其透明 // 导航栏颜色 navigationBarColor: \"#fd121de5\", // 状态栏颜色 statusBarColor: \"#ff0ad9c2\", // 导航栏文字颜色 navigationBarContentColor: \"#fde20606\", // 状态栏文字颜色 statusBarContentColor: \"#fff1e50a\" })*/ } build() { Row() { Column() { Text(this.message) .fontSize(50) .fontWeight(FontWeight.Bold) .onClick(()=>{ this.setSystemBar() }) } .width(\'100%\') } .height(\'100%\') .backgroundColor(Color.Brown) }}
2、HarmonyOS 如何实现全局页面置灰?
置灰可以通过选择设置页面根组件的如下通用属性.saturate(0)和.grayscale(1)
参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-universal-attributes-image-effect-V5
3、HarmonyOS 自定义弹窗组件,点击空白处弹窗消失?
参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-promptaction-V5#ZH-CN_TOPIC_0000001884757698__basedialogoptions11
4、HarmonyOS this.weightDao为空, 当使用UI组件作为参数时?
this.weightDao为空, 当使用UI组件作为参数时。(临时用new WeightHIstoryDao可以解决,如何修改使得this.weightDao在onRightClick中可用)
可以查看此文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-builderparam-V5,文档里有详细描述this指向的示例
当开发者创建了自定义组件,并想对该组件添加特定功能,例如想在某一个指定的自定义组件中添加一个点击跳转操作,此时若直接在组件内嵌入事件方法,将会导致所有该自定义组件的实例都增加了功能。为解决此问题,ArkUI引入了@BuilderParam装饰器,@BuilderParam用来装饰指向@Builder方法的变量(@BuilderParam是用来承接@Builder函数的)。开发者可以在初始化自定义组件时,使用不同的方式(如:参数修改、尾随闭包、借用箭头函数等)对@BuilderParam装饰的自定义构建函数进行传参赋值,在自定义组件内部通过调用@BuilderParam为组件增加特定的功能。该装饰器用于声明任意UI描述的一个元素,类似slot占位符。
5、HarmonyOS APP默认启动效果是否能够取消?
问题1:APP默认启动效果是否可以取消?开发者能否自定义启动页?即创建自己的LaunchAbility新页面。
问题2:默认启动效果中的图标(startWindowIcon)是否可以自定义图片大小?
问题1:APP默认启动效果是通过module.json5中startWindowIcon和startWindowBackground两个字段设置的,启动页是每个UIAbility都必须有的,这两个字段不可缺省,否则不能编译通过。如果想自定义启动页,可以将startWindowIcon设置为透明图片来实现取消。UIAbility组件启动页面图标和背景颜色设置,但是不是真正意义上的启动页。开发者可以自行手动创建splash页面进行业务初始化操作来实现自定义启动页,可以参考Splash Screen Ability 样例。
问题2:默认启动效果中的图标(startWindowIcon),你icon是实际多大启动展示就是多大,不会根据设备屏幕或窗口大小自适应调整。比如想设置全屏图片可以根据设备的全屏尺寸提供图片。