打开应用时使用display.getDefaultDisplaySync()方法获取屏幕尺寸,然后通过窗口的on('windowSizeChange')方法实现对窗口尺寸大小变化的监听。示例代码如下:
import { UIAbility} from '@kit.AbilityKit';
import { window } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';
export default class EntryAbility extends UIAbility {
onCreate(): void {
}
onDestroy(): void {
}
onWindowStageCreate(windowStage: window.WindowStage): void {
let windowClass: window.Window | undefined = undefined;
try {
window.getLastWindow(this.context, (err: BusinessError, data) => {
const errCode: number = err.code;
if (errCode) {
return;
}
windowClass = data;
try {
// 对窗口尺寸大小变化的监听
windowClass.on('windowSizeChange', (data) => {
AppStorage.setOrCreate('width', data.width);
AppStorage.setOrCreate('height', data.height);
});
} catch (exception) {
}
});
} catch (exception) {
}
windowStage.loadContent('pages/Index', (err) => {
if (err.code) {
return;
}
});
}
onWindowStageDestroy(): void {
}
onForeground(): void {
}
onBackground(): void {
}
} import display from '@ohos.display';
import { deviceInfo } from '@kit.BasicServicesKit';
let productSeriesInfo: string = deviceInfo.productSeries;
console.info('the value of the deviceInfo productSeries is :' + productSeriesInfo);
@Entry
@Component
struct Index {
@StorageLink('width') width1: number = 0;
@StorageLink('height') height1: number = 0;
aboutToAppear(): void {
let displayClass: display.Display | null = null;
displayClass = display.getDefaultDisplaySync();
this.width1 = displayClass.width;
this.height1 = displayClass.height;
}
build() {
Column() {
Text(this.width1.toString())
.fontSize(50)
.fontWeight(FontWeight.Bold)
Text(this.height1.toString())
.fontSize(50)
.fontWeight(FontWeight.Bold)
}
.height('100%')
.width('100%')
}
} 目前系统未提供直接判断折叠态的单一API,需通过屏幕尺寸变化监听或断点机制间接判断折叠/展开状态。另外,推荐使用断点作为折叠展开态布局判断条件。系统侧设计了横向和纵向断点分别代表窗口宽度和窗口高宽比,页面布局的一多要求使用横向和纵向断点进行判断实现,更多详情请参考一多断点开发实践与折叠屏开发实践。针对Pura X如何判断折叠展开态,可参考Pura X开发实践之【内屏适配】。