我最近遇到了同样的问题,使用 setAnchorView(videoView) 会将控制器完全设置在 VideoView 下,而不是悬停在它的底部区域。我的 VideoView 是上部区域的三分之一屏幕,因此控制器最终会覆盖 VideoView 下的任何 View。
以下是我最终在不编写成熟的自定义控制器的情况下完成它的方式(仅覆盖 MediaController 的 onSizeChanged 以向上移动锚点):
使用 FrameLayout 作为 MediaContoller 的锚点,将其与 VideoView 一起包装如下:
<RelativeLayout
android:id="@+id/videoLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.3"
android:layout_gravity="center"
android:background="#000000">
<VideoView
android:id="@+id/videoView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true" />
<FrameLayout android:id="@+id/controllerAnchor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
创建自定义 MediaController,当 FrameLayout 的大小发生变化时,它将向上移动 FrameLayout(固定在其上的 MediaController 将跟随):
public class MyMediaController extends MediaController
{
private FrameLayout anchorView;
public MyMediaController(Context context, FrameLayout anchorView)
{
super(context);
this.anchorView = anchorView;
}
@Override
protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld)
{
super.onSizeChanged(xNew, yNew, xOld, yOld);
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) anchorView.getLayoutParams();
lp.setMargins(0, 0, 0, yNew);
anchorView.setLayoutParams(lp);
anchorView.requestLayout();
}
}
使用上面的自定义控制器代替标准控制器,然后将其锚定到 FrameLayout :
protected void onCreate(Bundle savedInstanceState)
{
//...
videoView = (VideoView) findViewById(R.id.videoView1);
videoController = new MyMediaController(this, (FrameLayout) findViewById(R.id.controllerAnchor));
videoView.setMediaController(videoController);
//...
}
public void onPrepared(MediaPlayer mp)
{
videoView.start();
FrameLayout controllerAnchor = (FrameLayout) findViewById(R.id.controllerAnchor);
videoController.setAnchorView(controllerAnchor);
}