13

我希望能够显示一个按钮来开始一个视频,在视频将播放的同一视图内居中(使用 VideoView)。我还希望按钮在单击后消失,因为我正在使用 MediaController 类在视频开始后执行暂停、倒带、快进操作。

我该怎么做呢?

这是我到目前为止的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/LinearLayout1"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical">

<FrameLayout
  android:id="@+id/video_frame"
  android:layout_width="fill_parent"
  android:layout_height="480px"
  android:background="#000"
  >

  <VideoView
    android:id="@+id/video_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />

</FrameLayout>

我尝试以编程方式将 ImageButton 添加到 FrameLayout,但这似乎不起作用。

4

3 回答 3

14

好的,这就是我解决这个问题的方法。布局文件非常简单。只需在 VideoView 元素添加一个 ImageButton :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/LinearLayout1"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical">

<FrameLayout
  android:id="@+id/video_frame"
  android:layout_width="fill_parent"
  android:layout_height="480px"
  android:background="#000"
  >

  <VideoView
    android:id="@+id/video_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />

  <ImageButton
    android:id="@+id/play_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical|center_horizontal"
    android:src="@drawable/ic_launcher"
    />

</FrameLayout>

FrameLayout 视图元素按照您在布局中定义它们的顺序将其子元素层叠在一起。因此,布局中添加的最后一个元素被绘制在顶部。注意 ImageButton 有这个属性:

android:layout_gravity="center_vertical|center_horizontal"

此属性使 ImageButton 在 FrameLayout 中居中。

下一个技巧是让 ImageButton 在单击后消失。使用 ImageButton 上的 setVisibility() 方法来执行此操作:

    // Setup a play button to start the video
    mPlayButton = (ImageButton) findViewById(R.id.play_button);
    mPlayButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (mPlayer.isPlaying()) {
                resetPlayer();
            } else {
                playVideo(videoUrl, mVideoView.getHolder());
                // show the media controls
                mController.show();
                // hide button once playback starts
                mPlayButton.setVisibility(View.GONE);
            }
        }
    });
于 2013-03-09T01:36:32.597 回答
1

有一个鲜为人知的功能FrameLayout称为Foreground Drawable。您可以指定将在所有 FrameLayout 子项之上呈现的可绘制对象。所以:

mFrameLayout.setForegroundDrawable(mPlayDrawable);

会成功的,你的布局会更有效率(更少的视图)。

您可以使用 Gravity 常量将可绘制对象与

mFrameLayout.setForegroundGravity(Gravity.XXXX)
于 2014-04-02T17:21:41.567 回答
0

尝试制作 FrameLayout Clickable,而不是使用按钮。

或者,您可以在 FrameLayout 下方放置一个 Button,然后在 onClick 事件处理程序中,将 View 的可见性设置为 GONE。

编辑:或尝试这样的事情:https ://stackoverflow.com/a/7511535/826731

于 2013-03-09T01:30:53.093 回答