【发布时间】:2016-03-22 20:29:13
【问题描述】:
我正在开发一个 Android 上的应用程序,我想通过单击图像来播放 youtube 视频。我不想移动到不同的活动或屏幕。
另外,除了分配给图像的空间之外,我不希望视频占用任何额外空间。
有人可以提出任何解决方案吗?我没有找到任何关于 Android 的帖子。
【问题讨论】:
标签: android android-layout youtube-api
我正在开发一个 Android 上的应用程序,我想通过单击图像来播放 youtube 视频。我不想移动到不同的活动或屏幕。
另外,除了分配给图像的空间之外,我不希望视频占用任何额外空间。
有人可以提出任何解决方案吗?我没有找到任何关于 Android 的帖子。
【问题讨论】:
标签: android android-layout youtube-api
假设在 OnClickListener 中,你捕获了点击事件:
1) 你可以通过调用setVisibility(View.GONE) 使图像不可见。
2) 您将拥有一个扩展 YouTubePlayerSupportFragment 的 VideoFragment 类,并且它将包含在一个不可见的容器中,因此您可以调用
VideoFragment videoFragment = (VideoFragment) getFragmentManager().findFragmentById(R.id.video_fragment_container);
videoFragment.setVideoId(videoId);
container.setVisibility(View.VISIBLE);
【讨论】:
感谢您的帮助。这对我的应用程序很有效。让我解释一下我最终是如何做到的。为此,我们需要相对布局,我们可以在其中重叠视图。我已经重叠了 ImageView 和 youtubeView。最初,我将 ImageView 设置为可见,将 youtubeView 设置为不可见。在 ImageView 的 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);
}
});
【讨论】: