调整屏幕旋转

This commit is contained in:
DevWiki 2024-04-08 15:55:48 +08:00
parent 5e4df7785f
commit 498cdaedb1
2 changed files with 7 additions and 11 deletions

View File

@ -6,7 +6,8 @@ import { window } from '@kit.ArkUI';
@CustomDialog
struct WebViewDialog {
showHeight: Length = '50%';
@StorageLink(ScreenUtil.isPortraitKey)isPortrait: boolean = true;
dialogController: CustomDialogController = new CustomDialogController({
builder: WebViewDialog({})
});
@ -14,7 +15,7 @@ struct WebViewDialog {
build() {
WebPage({onTitleBarLeftClick: ()=> {
this.dialogController.close();
}}).height(this.showHeight).width('100%')
}}).height(this.isPortrait ? '95%' : '100%').width(this.isPortrait ? '100%' : '50%')
}
}
@ -23,10 +24,9 @@ struct WebViewDialog {
struct Index {
@State title: string = "Home";
@StorageLink(ScreenUtil.isPortraitKey)isPortrait: boolean = true;
dialogController: CustomDialogController = new CustomDialogController({
builder: WebViewDialog({showHeight: this.isPortrait ? '95%' : '50%'}),
builder: WebViewDialog(),
alignment: DialogAlignment.BottomEnd,
customStyle: true
})
@ -36,7 +36,7 @@ struct Index {
}
onPageShow(): void {
ScreenUtil.getInstance().setPreferredOrientation(window.Orientation.AUTO_ROTATION_PORTRAIT);
ScreenUtil.getInstance().setPreferredOrientation(window.Orientation.AUTO_ROTATION);
}
onTitleBarLeftClick(_event: ClickEvent) {

View File

@ -15,7 +15,6 @@ export class ScreenUtil {
public layoutHeight: number = 0
private landscapeListener: mediaquery.MediaQueryListener = mediaquery.matchMediaSync('(orientation: landscape)');
private portraitListener: mediaquery.MediaQueryListener = mediaquery.matchMediaSync('(orientation: portrait)');
public static getInstance(): ScreenUtil {
@ -26,11 +25,8 @@ export class ScreenUtil {
}
initScreenSize(): void {
this.landscapeListener.on('change', ()=> {
AppStorage.setOrCreate(ScreenUtil.isPortraitKey, false)
});
this.portraitListener.on('change', ()=> {
AppStorage.setOrCreate(ScreenUtil.isPortraitKey, true)
this.portraitListener.on('change', (result)=> {
AppStorage.setOrCreate(ScreenUtil.isPortraitKey, result.matches)
})
window.getLastWindow(getContext(this))
.then((windowClass: window.Window) => {