HarmonyOS鸿蒙Next中Video组件如何跟图片组件一样实现宽度100%高度不设置自适应
HarmonyOS鸿蒙Next中Video组件如何跟图片组件一样实现宽度100%高度不设置自适应
开发者您好,根据您回复中的效果图,请问一下您是否是想实现点击立即创作后,实现全屏沉浸式显示效果?如果是这样,您可参考官网文档:如何实现沉浸式页面(包括沉浸式状态栏、沉浸式导航条)。
具体在使用Video场景下实现,您可以参考以下例子,进入全屏状态,进入窗口全屏布局,同时修改状态文字的颜色,避免与背景颜色相接近:
@Entry
@Component
struct Index {
private controller = new VideoController();
private context = this.getUIContext().getHostContext() as common.UIAbilityContext;
private windowClass = (this.context as common.UIAbilityContext).windowStage.getMainWindowSync();
// 记录当前是否全屏状态
@State private isFullScreen: boolean = false;
build() {
Stack() {
Video({
src: $rawfile('test1.mp4'), // 替换为您的本地视频文件路径
controller: this.controller
})
.width('100%')
.height(this.isFullScreen ? '100%' : 300)
.objectFit(ImageFit.Cover)
.autoPlay(true)
.loop(true)
.controls(false);
// 自定义的控制器,全屏切换
Row() {
Text(this.isFullScreen ? '退出全屏' : '进入全屏')
.fontColor(Color.White);
}
.border({ color: Color.White, width: 1 })
.margin({ bottom: 15 })
.zIndex(1)
.onClick(() => {
this.isFullScreen = !this.isFullScreen;
this.toggleFullscreen(this.isFullScreen);
});
}
.alignContent(Alignment.Bottom)
.width('100%');
}
// 切换全屏,修改状态栏文字颜色
private toggleFullscreen(isFullScreen: boolean) {
// 1. 设置窗口全屏布局
this.windowClass.setWindowLayoutFullScreen(isFullScreen).then(() => {
console.info('Succeeded in setting the window layout to full-screen mode.');
}).catch((err: BusinessError<void>) => {
console.error(`Failed to set the window layout to full-screen mode. Code is ${err.code}, message is ${err.message}`);
});
// 2. 修改状态栏文字颜色
let systemBarProperties: window.SystemBarProperties = {
statusBarContentColor: '#fff4f4f6' // 状态栏文字颜色
};
this.windowClass.setWindowSystemBarProperties(systemBarProperties).then(() => {
console.info('Succeeded in setting the system bar properties.');
}).catch((err: BusinessError<void>) => {
console.error(`Failed to set systemBar properties. Cause code: ${err.code}, message: ${err.message}`);
});
}
}
若未能解决您的问题,请您再反馈。
更多关于HarmonyOS鸿蒙Next中Video组件如何跟图片组件一样实现宽度100%高度不设置自适应的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
这是UI展示效果

Stack() {
Video({
src: String(this.obj?.resourceUrlV2),
previewUri: this.obj?.resourcePreviewUrl,
controller: this.VideoController
})
.autoPlay(true)
.objectFit(ImageFit.Cover)
.width('100%')
.controls(false)
.onStart(() => {
this.is_show_btn = false
})
.onFinish(() => {
console.log('播放结束')
this.isEnd = true
})
if (this.isEnd) {
Column() {
Image($rawfile('ai_video/reset.png')).width(60).height(60).onClick(() => {
this.isEnd = false
this.VideoController.start()
})
Text('重播').fontSize(14).fontColor('#FFFFFF').fontWeight(500).margin({ top: 10 })
}.width('100%').height('100%').justifyContent(FlexAlign.Center)
}
if (this.is_show_btn) {
Column() {
Image($r('app.media.loading')).onAppear(() => {
this.angle = 360
}).width(40).rotate({ angle: this.angle }).animation({
duration: 1000,
iterations: -1,
})
}.width('100%').height('100%').justifyContent(FlexAlign.Center)
}
}.width('100%')
你现在 Video 组件应该就是 自适应撑开的 把 ,可以看看你的UI展示效果吗?,
如何解决“无法加载文件,因为在此系统上禁止运行脚本”错误
错误描述
在Windows PowerShell中运行脚本时,可能会遇到以下错误:
无法加载文件,因为在此系统上禁止运行脚本。有关详细信息,请参阅 "get-help about_signing"。
所在位置 行:1 字符: 1
错误原因
此错误是由于PowerShell的执行策略(Execution Policy)设置导致的。默认情况下,PowerShell的执行策略设置为“Restricted”(受限制),这会阻止运行任何脚本文件(.ps1文件)。
解决方法
方法一:以管理员身份运行PowerShell并更改执行策略
- 以管理员身份打开PowerShell
- 输入以下命令更改执行策略:
Set-ExecutionPolicy RemoteSigned - 输入
Y确认更改
方法二:为当前用户更改执行策略
如果不想更改全局执行策略,可以仅为当前用户更改:
Set-ExecutionPolicy -Scope CurrentUser RemoteSigned
方法三:临时更改执行策略(仅当前会话有效)
Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process
方法四:使用Bypass参数运行脚本
powershell -ExecutionPolicy Bypass -File script.ps1
执行策略说明
- Restricted:默认设置,禁止运行任何脚本
- AllSigned:只运行由受信任发布者签名的脚本
- RemoteSigned:运行本地脚本,远程脚本需要签名(推荐)
- Unrestricted:运行所有脚本(不推荐)
- Bypass:不阻止任何操作,无警告提示
注意事项
- 更改执行策略可能会带来安全风险,请确保只运行可信的脚本
- 推荐使用
RemoteSigned策略,它允许运行本地脚本,但对远程脚本要求签名 - 如果只需要临时运行脚本,建议使用方法三或方法四
验证更改
更改后,可以使用以下命令验证当前执行策略:
Get-ExecutionPolicy
def hello_world():
print("Hello, World!")
if __name__ == "__main__":
hello_world()
有要学HarmonyOS AI的同学吗,联系我:https://www.itying.com/goods-1206.html
Stack() {
Video({
src: String(this.obj?.resourceUrlV2),
previewUri: this.obj?.resourcePreviewUrl,
controller: this.VideoController
})
.autoPlay(true)
.objectFit(ImageFit.Cover)
.width('100%')
.controls(false)
.onStart(() => {
this.is_show_btn = false
})
.onFinish(() => {
console.log('播放结束')
this.isEnd = true
})
if (this.isEnd) {
Column() {
Image($rawfile('ai_video/reset.png')).width(60).height(60).onClick(() => {
this.isEnd = false
this.VideoController.start()
})
Text('重播').fontSize(14).fontColor('#FFFFFF').fontWeight(500).margin({ top: 10 })
}.width('100%').height('100%').justifyContent(FlexAlign.Center)
}
if (this.is_show_btn) {
Column() {
Image($r('app.media.loading')).onAppear(() => {
this.angle = 360
}).width(40).rotate({ angle: this.angle }).animation({
duration: 1000,
iterations: -1,
})
}.width('100%').height('100%').justifyContent(FlexAlign.Center)
}
}.width('100%')
你可以单开一楼发 这个格式影响看了,
好的,
在HarmonyOS Next中,Video组件可通过设置layoutWeight属性实现宽度100%并高度自适应。具体代码示例如下:
Video({
src: $rawfile('video.mp4')
})
.width('100%')
.aspectRatio(16/9) // 根据视频宽高比设置
.layoutWeight(1) // 在容器中自适应
关键点:
- width设为’100%'撑满容器宽度
- aspectRatio设置视频原始宽高比(如16:9)
- layoutWeight确保在弹性布局中自适应高度
- 父容器需设置合适的布局约束
这样视频宽度会占满容器,高度根据宽高比自动计算,保持视频原始比例不变形。
在HarmonyOS Next中,Video组件要实现宽度100%、高度自适应,核心是设置aspectRatio属性。
具体实现如下:
-
设置宽度为100%:
.width('100%') -
设置自适应高度:
.aspectRatio(16/9) // 根据视频实际宽高比设置
完整示例代码:
Video({
src: $rawfile('video.mp4'),
controller: this.videoController
})
.width('100%')
.aspectRatio(16/9) // 关键:设置视频宽高比来自适应高度
注意事项:
aspectRatio的值应为视频的实际宽高比(如16:9、4:3等)- 如果不设置
aspectRatio,Video组件默认使用视频源的实际尺寸 - 这种方式与Image组件的
objectFit设置为Cover或Contain时的自适应效果类似
通过这种方式,Video组件就能实现宽度撑满容器、高度按比例自适应的效果了。

