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

Android Studio:长于屏幕的视频无法滚动,如何实现上下滚动?

实现超长视频的上下滚动功能

你的问题核心是当前布局缺少可滚动容器,导致超出屏幕的视频内容无法被查看。只需将VideoView嵌套到可滚动容器组件中即可解决,以下是具体实现步骤:

1. 修改布局文件(activity_main.xml)

将VideoView放入ScrollView(或NestedScrollView,适配更复杂嵌套场景)中,注意ScrollView仅能有一个直接子视图,可保留原有外层LinearLayout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <VideoView
            android:id="@+id/dig_lic"
            android:layout_width="wrap_content"
            android:layout_height="2950px" />
    </ScrollView>
</LinearLayout>

2. 代码逻辑无需调整(MainActivity.kt)

原有的视频循环播放逻辑可保持不变,滚动功能由布局容器提供,与视频播放逻辑无冲突:

class MainActivity: ComponentActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val videoView = findViewById<VideoView>(R.id.dig_lic)
    val packageName = "android.resource://" + getPackageName() + "/" + R.raw.dig_lic_video
    val uri = Uri.parse(packageName)
    videoView.setOnPreparedListener {
      it.isLooping = true
    }
    videoView.setVideoURI(uri)
    videoView.start()
  }
}

额外注意事项

  • 若视频为竖屏长视频,需确保VideoView的layout_height设置为视频实际高度(你当前设置的2950px是可行的),这样ScrollView才能识别内容超出屏幕范围,触发滚动。
  • 若后续需要配合AppBarLayout等滑动组件,建议使用NestedScrollView替代ScrollView,避免滑动冲突。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 19:01:00