如何在点击图片时播放YouTube视频?

时间:2016-03-22 20:29:13

标签: android android-layout youtube-api

我正在使用Android上的应用程序,我想在点击图片时播放YouTube视频。我不想进入不同的活动或屏幕。

另外,我不希望视频占用分配给图像的空间以外的任何额外空间。

有人可以建议任何解决方案吗?我没有找到任何关于Android的帖子。

2 个答案:

答案 0 :(得分:1)

假设在OnClickListener中,您捕获click事件:

1)您可以通过调用setVisibility(View.GONE)来使图像不可见。

2)你会有一个扩展VideoFragment的{​​{1}}类,它将被包含在一个不可见的容器中,所以你可以调用

YouTubePlayerSupportFragment

有关详细信息,请查看the sample applications

答案 1 :(得分:1)

感谢您的帮助。这确实适用于我的应用程序。让我解释一下我是如何做到的。为此,我们需要相对布局,我们可以在其中重叠视图。我重叠了ImageView和youtubeView。最初,我将ImageView设置为VISIBLE,将youtubeView设置为INVISIBLE。在ViewView的View.onClickListener中,我将ImageView设为INVISIBLE,将youtubeView设为VISIBLE。这对我很有用。

注意:只有在RelativeLayout中才能重叠视图,除非您想要创建自己的自定义布局,否则不能在任何其他布局中使用。

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/listPreferredItemHeight"
        android:paddingStart="@dimen/activity_horizontal_margin_less"
        android:paddingRight="@dimen/activity_horizontal_margin_less"
        android:paddingTop="@dimen/activity_vertical_margin_less"
        android:paddingBottom="@dimen/activity_vertical_margin_less"
        tools:context="com.example.puneet.movieout.MovieInfoDisplay"
        >


        <TextView
            android:layout_width="wrap_content"
            android:id="@+id/textView2"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:text="I am Puneet Chugh. I am Puneet Chugh"
            android:layout_centerHorizontal="true"
            android:textSize="24sp"
            android:textStyle="bold"
            android:textColor="@color/black"/>

        <com.google.android.youtube.player.YouTubePlayerView
            android:id="@+id/youtube_view"
            android:layout_below="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"

        />
        <ImageView
            android:layout_marginTop="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:id="@+id/imageView"
            android:layout_below="@+id/textView2"/>
    </RelativeLayout>

活动部分:

    youTubeView.setVisibility(View.INVISIBLE);

    moviePoster.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            moviePoster.setVisibility(View.INVISIBLE);
            youTubeView.setVisibility(View.VISIBLE);
        }
    });