通过配置module.json5文件中abilities标签的maxWindowWidth、maxWindowHeight两个属性,设置窗口设置宽高为500*1000,实际运行过程中未生效,日志监听窗口大小为950 * 1394。
示例代码:
import { window } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';
import { common } from '@kit.AbilityKit';
@Entry
@Component
struct Index {
@State screenWidth: number = 0;
@State screenHeight: number = 0;
context: Context | undefined = undefined;
getScreenInfo() {
let windowClass: window.Window | undefined = undefined;
try {
let promise = window.getLastWindow(this.context);
promise.then((data) => {
windowClass = data;
this.screenWidth = windowClass.getGlobalRect().width; // 获取窗口宽度
this.screenHeight = windowClass.getGlobalRect().height; // 获取窗口高度
}).catch((err: BusinessError) => {
console.error('getLastWindow error', err);
});
} catch (e) {
console.error('setScreenOrientation error');
}
}
aboutToAppear(): void {
this.context = this.getUIContext().getHostContext() as common.Context;
this.getScreenInfo();
}
build() {
Column() {
Text(`宽度是:${this.getUIContext().px2vp(this.screenWidth)},高度是:${this.getUIContext()
.px2vp(this.screenHeight)}`)
}
}
} {
"module": {
"name": "entry",
"type": "entry",
"description": "$string:module_desc",
"mainElement": "EntryAbility",
"deviceTypes": [
"phone",
"2in1"
],
"deliveryWithInstall": true,
"installationFree": false,
"pages": "$profile:main_pages",
"abilities": [
{
"name": "EntryAbility",
"srcEntry": "./ets/entryability/EntryAbility.ets",
"description": "$string:EntryAbility_desc",
"icon": "$media:layered_image",
"label": "$string:EntryAbility_label",
"startWindowIcon": "$media:startIcon",
"startWindowBackground": "$color:start_window_background",
"maxWindowHeight": 1000, // 设置最大高度
"minWindowHeight": 1000, // 设置最小高度
"maxWindowWidth": 500, // 设置最大宽度
"minWindowWidth": 500, // 设置最小宽度
"exported": true,
"skills": [
{
"entities": [
"entity.system.home"
],
"actions": [
"ohos.want.action.home"
]
}
]
}
],
"extensionAbilities": [
{
"name": "EntryBackupAbility",
"srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets",
"type": "backup",
"exported": false,
"metadata": [
{
"name": "ohos.extension.backup",
"resource": "$profile:backup_config"
}
],
}
]
}
} Q:PC端应用如何设置窗口大小?
A:PC/2in1设备上的应用可以支持全屏或自由窗口、自定义窗口标题栏以及沉浸式体验,满足用户的多样化体验。