如何在 Android Studio 中将一个 VideoView 放在另一个 VideoView 下方?

How can I place one VideoView below another VideoView in Android Studio?

我需要创建一个视频视图,它应该位于 运行 时间 ID 为 "video1" 的视频视图的正下方。 该代码可以很好地创建视图,但我无法将它放在 ID 为 "video1" 的 videoView 下方。这是代码:

            RelativeLayout layout = findViewById(R.id.layout);
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) 
            layout.getLayoutParams();
            params.addRule(RelativeLayout.BELOW, R.id.video1);
            videoView2 = new VideoView(jazz.this);
            videoView2.setLayoutParams(params);
            videoView2.setLayoutParams(new 
            FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, 200));
            layout.addView(videoView2);

这是您的代码:

 videoView2.setLayoutParams(params);
        videoView2.setLayoutParams(new 
        FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, 200))

您设置了两次 LayoutParams...

使用这个:

 VideoView v = new VideoView(context);
    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 200);
    layoutParams.addRule(RelativeLayout.BELOW, R.id.video1);
    v.setLayoutParams(layoutParams);
  container.addView(v);

会成功的。希望能帮到你

您应该像这样为视频 2 设置参数:

RelativeLayout layout = (RelativeLayout)findViewById(R.id.layout);
        videoView2 = new VideoView(this);
        videoView2.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,200));
        layout.addView(videoView2);
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) videoView2.getLayoutParams();
        params.addRule(RelativeLayout.BELOW, R.id.video1);
        videoView2.setLayoutParams(params);

并且您的视频 1 应该设置一个高度参数,因为 videoview 测量是基于您设置的视频,从外观上看,没有定义视频路径,所以如果视频 1 高度=wrap_content,它占据了整个屏幕,没有边界。我的 xml 是这样的:

<VideoView
    android:id="@+id/video1"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:layout_marginBottom="20dp"/>