java - 使用相对布局和按钮将一个 videoView 覆盖在另一个 videoView 上

标签 java android xml android-studio android-layout

我对 Android Studio 相当陌生,正在尝试一个项目,其中基于点击事件,videoView(2 个视频)从 vid1 变为 vid2,vid2 变为 vid1 等等,并且两者同时播放且长度相同。当我使用内置函数 BringToFront() 时,视频不会交换,我只能看到第一个视频。

Button button;
        button = (Button) findViewById(R.id.button);
        vd1 = (VideoView) findViewById(R.id.videoView6);
        vd1.setVideoURI(Uri.parse("android.resource://com.example.venapp/" + R.raw.first));
        vd1.start();
        vd1.setOnCompletionListener (new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                vd1.start();
            }
        });

        vd2 = (VideoView) findViewById(R.id.videoView5);
        vd2.setVideoURI(Uri.parse("android.resource://com.example.venapp/" + R.raw.second));
        vd2.start();
        vd2.setOnCompletionListener (new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                vd2.start();
            }
        });

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (counter % 2 == 0) { // even # of click
                    counter++;
                    vd2.bringToFront();
                }
                else if (counter % 2 != 0) { // odd # of click
                    counter++;
                    vd1.bringToFront();
                }
            }
        });
<RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginTop="60dp"
        android:layout_marginBottom="140dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <VideoView
            android:id="@+id/videoView5"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true" />

        <VideoView
            android:id="@+id/videoView6"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true" />

        <Button
            android:id="@+id/button"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:background="#00FFFFFF" />

    </RelativeLayout>

最佳答案

您可以使用 setVisibility() 以及参数 View.VISIBLE 来设置整个 View 的可见性,而不是使用 bringToFront() >、View.INVISIBLEView.GONE

要达到您想要的效果,可以使用setVisibility(View.GONE)setVisibility(View.VISIBLE)

Android 文档:https://developer.android.com/reference/android/view/View#setVisibility(int)

关于java - 使用相对布局和按钮将一个 videoView 覆盖在另一个 videoView 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60824650/

相关文章:

android - 具有单选模式的 ListView 希望自动选择第一项并调用其 onitemclicklistener

xml - xsl :fo - how to split for-each on pages

java - 如何使用Config.getPhotoPath()?

java - 在 ant junit 任务中更改工作目录

Android Activity 和 Service 通信 : how to keep the UI up-to-date

Android:我可以重复使用图层列表吗?

c# - 在 C# 中读取远程 XML

Java 8 lambda 泛型接口(interface)方法

java - 如何从 Volley 请求响应中解析大型 JsonArray

android - 如何在android中动态授予权限,即在运行应用程序时,这是可以实现的吗?