【发布时间】:2012-06-06 19:44:22
【问题描述】:
我正在学习教程并尝试通过 Android 的 VideoView 构建自定义视频播放器。
现在,当我触摸 VideoView 时,我想显示或隐藏媒体控制器。如果控制器已经显示,则隐藏它们,如果它们被隐藏,则显示它们。
这些媒体控制器由 2 个位于 VideoView 上方的 LinearLayout 表示。 以红色突出显示的区域代表背景中的 VideoView 边框。
我的问题是,当我触摸与 VideoView 重叠的媒体控制器区域时,媒体控制器会被隐藏。这不是想要的效果,因为我不是直接接触 VideoView,而是 LinearLayout。
那么,为什么当我点击 LinearLayouts 时 Videoview 会捕捉到触摸事件?
这是我的onTouch 实现:
@Override
public boolean onTouch(View v, MotionEvent event) {
if (!isMediaControlerShown) {
topPanel.setVisibility(View.VISIBLE);
bottomPanel.setVisibility(View.VISIBLE);
isMediaControlerShown = true;
} else {
topPanel.setVisibility(View.GONE);
bottomPanel.setVisibility(View.GONE);
isMediaControlerShown = false;
}
return false;
}
以及XML布局的架构:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<VideoView
android:id="@+id/videoView"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:layout_height="wrap_content" />
<!-- The top panel of the Video Player -->
<LinearLayout
android:visibility="gone"
android:id="@+id/top_panel"
style="@style/VideoTopPanel"
android:orientation="vertical" >
<!-- ////// -->
</LinearLayout>
<!-- The bottom panel of the Video Player -->
<LinearLayout
android:visibility="gone"
android:id="@+id/bottom_panel"
xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/VideoBottomPanel"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:paddingBottom="@dimen/video_bottom_panel_padding_bottom"
android:paddingTop="@dimen/video_bottom_panel_padding_top" >
<!-- ////// -->
</LinearLayout>
</RelativeLayout>
【问题讨论】:
-
让
onTouch返回true 而不是false,因为您总是在处理触摸事件。如果您返回false,它会向下级联到下一个级别,直到找到true。 -
感谢您的评论。它没有帮助。行为是这样的:在动作 ACTION_DOWN 时媒体控制器出现,而在 ACTION_UP 时消失。所以控制器的冲洗速度非常快。
标签: android android-layout android-intent android-emulator android-widget