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

如何解决BottomSheetDialog内VideoView的MediaController被遮挡问题

问题根源

MediaController默认会被添加到当前Activity的根DecorView中,而BottomSheetDialog是独立的Window,z轴层级高于Activity的基础Window,所以MediaController会被BottomSheet整体遮挡,即使位置计算正确也显示不出来。


解决方案

步骤1:修改VideoView对应的布局文件,给父容器添加ID,方便后续挂载MediaController

<?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:id="@+id/videoContainer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <VideoView
        android:id="@+id/videoView"
        android:layout_width="wrap_content"
        android:layout_height="210dp"
        android:layout_marginTop="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:clickable="true"/>

</androidx.constraintlayout.widget.ConstraintLayout>

步骤2:修改PageVideoEpoxyModel中的MediaController创建逻辑,手动指定挂载父容器

data class PageVideoEpoxyModel(
    val videoUrl: String
):ViewBindingKotlinModel<ActivityPageVideoBinding>(R.layout.activity_page_video){

    override fun ActivityPageVideoBinding.bind() {
        videoView.setVideoURI(Uri.parse(videoUrl))
        videoView.setOnPreparedListener {
            val mediaController = MediaController(videoView.context)
            mediaController.setMediaPlayer(videoView)
            mediaController.isEnabled = true
            videoView.setMediaController(mediaController)
            
            // 核心修改:手动将MediaController添加到VideoView的父容器中
            videoContainer.addView(mediaController)
            val controllerParams = mediaController.layoutParams as ConstraintLayout.LayoutParams
            controllerParams.apply {
                width = ConstraintLayout.LayoutParams.MATCH_PARENT
                height = ConstraintLayout.LayoutParams.WRAP_CONTENT
                bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID
                startToStart = ConstraintLayout.LayoutParams.PARENT_ID
                endToEnd = ConstraintLayout.LayoutParams.PARENT_ID
            }
            mediaController.layoutParams = controllerParams
            
            mediaController.setAnchorView(videoView)
            mediaController.requestFocus()
            videoView.start()
            mediaController.show(0)
        }
    }
}

可选兜底方案

如果上述修改完成后仍有遮挡,可在创建BottomSheetDialog的逻辑中添加Window层级调整代码:

bottomSheetDialog.show()
// 新增以下代码
bottomSheetDialog.window?.let { window ->
    val layoutParams = window.attributes
    layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL
    window.attributes = layoutParams
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 05:36:03