调整屏幕旋转

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 @CustomDialog
struct WebViewDialog { struct WebViewDialog {
showHeight: Length = '50%'; @StorageLink(ScreenUtil.isPortraitKey)isPortrait: boolean = true;
dialogController: CustomDialogController = new CustomDialogController({ dialogController: CustomDialogController = new CustomDialogController({
builder: WebViewDialog({}) builder: WebViewDialog({})
}); });
@ -14,7 +15,7 @@ struct WebViewDialog {
build() { build() {
WebPage({onTitleBarLeftClick: ()=> { WebPage({onTitleBarLeftClick: ()=> {
this.dialogController.close(); 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 { struct Index {
@State title: string = "Home"; @State title: string = "Home";
@StorageLink(ScreenUtil.isPortraitKey)isPortrait: boolean = true;
dialogController: CustomDialogController = new CustomDialogController({ dialogController: CustomDialogController = new CustomDialogController({
builder: WebViewDialog({showHeight: this.isPortrait ? '95%' : '50%'}), builder: WebViewDialog(),
alignment: DialogAlignment.BottomEnd, alignment: DialogAlignment.BottomEnd,
customStyle: true customStyle: true
}) })
@ -36,7 +36,7 @@ struct Index {
} }
onPageShow(): void { onPageShow(): void {
ScreenUtil.getInstance().setPreferredOrientation(window.Orientation.AUTO_ROTATION_PORTRAIT); ScreenUtil.getInstance().setPreferredOrientation(window.Orientation.AUTO_ROTATION);
} }
onTitleBarLeftClick(_event: ClickEvent) { onTitleBarLeftClick(_event: ClickEvent) {

View File

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