You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Android Studio XML中启用VideoView?Kotlin项目需对应XML配置代码

可直接用于视频展示的原生视图组件

Android 原生提供的 VideoView 是和 ImageView、TextView 等同属 View 子类的标准视频展示组件,无需引入额外第三方依赖即可直接在布局中声明使用,符合你的使用需求。

XML 布局配置示例

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <VideoView
        android:id="@+id/videoView"
        android:layout_width="match_parent"
        android:layout_height="320dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginHorizontal="16dp"
        android:layout_marginTop="24dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Kotlin 侧基础使用示例

// 绑定布局中的VideoView
val videoView = findViewById<VideoView>(R.id.videoView)
// 配置媒体控制器,自带播放暂停、进度拖动、时长显示能力
val mediaController = MediaController(this)
mediaController.setAnchorView(videoView)
videoView.setMediaController(mediaController)
// 设置视频源,支持本地资源、本地文件路径、在线视频流地址
// 示例加载res/raw目录下的test_video.mp4资源
videoView.setVideoPath("android.resource://${packageName}/${R.raw.test_video}")
// 启动播放
videoView.start()

注意:如果需要播放在线视频,需要在 AndroidManifest.xml 中添加网络权限声明:<uses-permission android:name="android.permission.INTERNET" />

内容的提问来源于stack exchange,提问作者sabanayagamT

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 17:45:00