名称 | 描述 |
|---|---|
Visible | 组件状态为可见。 |
Hidden | 组件状态为不可见,但参与布局、进行占位。 |
None | 组件状态为不可见,不参与布局、不进行占位。 |
当使用Web组件时,在点击全屏触发onFullScreenEnter回调的时候,视频区域会被Web之外的组件占用,导致页面布局异常。
当Web组件与其他组件在一个容器的场景下,点击Web组件中调用的H5视频中的全屏会发生布局错乱的情况,此时可以通过修改一些布局属性,让视频充满全屏幕,达到全屏效果,或者根据实际业务场景对Web之外的组件进行显隐操作。
import { webview } from '@kit.ArkWeb'; @Entry @Component struct SolutionOne { controller: webview.WebviewController = new webview.WebviewController(); CONSTANT_HEIGHT = 150; // Web组件的高度默认值设置为150 @State marginTop: number = this.CONSTANT_HEIGHT; // 自定义组件的margin高度属性为全局变量 build() { Stack({ alignContent: Alignment.TopStart }) { Text('TextTextTextText') .width('100%') .height(this.CONSTANT_HEIGHT) .backgroundColor('#e1dede'); Web({ // 需替换为带有视频的网址才可达到预期效果 src: 'www.example.com', controller: this.controller }) .onFullScreenEnter(() => { console.info('onFullScreenEnter'); // 当全屏的时候,web组件的margin属性高度设置为0 this.marginTop = 0; }) .onFullScreenExit(() => { console.info('onFullScreenExit'); // 当退出全屏的时候,web组件的margin属性高度恢复初始值 this.marginTop = this.CONSTANT_HEIGHT; }) .width('100%') .height('100%') .zIndex(10) // Web组件margin属性高度呈动态变化 .margin({ top: this.marginTop }) .fileAccess(false) .geolocationAccess(false) .zoomAccess(true); } .width('100%') .height('100%'); } }
import { webview } from '@kit.ArkWeb'; @Entry @Component struct SolutionTwo { controller: webview.WebviewController = new webview.WebviewController(); CONSTANT_HEIGHT = 100; @State isVisible: boolean = true; // 自定义标志位isVisible,来控制是否需要显示组件 build() { Column() { Text('TextTextTextText') .width('100%') .height(this.CONSTANT_HEIGHT) .backgroundColor('#e1dede') // 当isVisible标志位为true的时候,组件状态为可见,否则组件状态为不可见,不参与布局、不进行占位 .visibility(this.isVisible ? Visibility.Visible : Visibility.None); Web({ // 需替换为带有视频的网址才可达到预期效果 src: 'www.example.com', controller: this.controller }) .onFullScreenEnter(() => { console.info('onFullScreenEnter'); // 当全屏的时候,isVisible标志位为false,组件状态为不可见,不参与布局、不进行占位 this.isVisible = false; }) .onFullScreenExit(() => { console.info('onFullScreenExit'); // 当退出全屏的时候,isVisible标志位为true,组件状态为可见 this.isVisible = true; }) .width('100%') .height('100%') .zIndex(10) .fileAccess(false) .geolocationAccess(false) .zoomAccess(true); } .width('100%') .height('100%'); } }
通过以上方法实现后,运行程序,可以看到点击全屏,视频正常显示,无布局错乱情况。