安卓中的导航抽屉和视频视图

18

我正在使用导航抽屉和选项卡布局。在我的选项卡中有一个视频,一开始它是不可见的,但我能听到声音。一旦我设置了

video_view.setZOrderOnTop(true);

我也能看到视频,但这会导致导航抽屉出现 问题

当我滑动它时,视频不像其他所有元素一样隐藏在导航抽屉后面。

输入图像描述

如果我不使用

video_view.setZOrderOnTop(true);

那么我的抽屉就能正常工作了。

main_activity.xml

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- Framelayout to display Fragments -->
<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/menuwhite2"
                    android:id="@+id/custom_home"
                    android:gravity="center"
                    android:layout_gravity="right"
                    android:layout_marginRight="10dp"/>

                <android.support.design.widget.TabLayout
                    android:id="@+id/tabs"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:tabMode="fixed"
                    app:tabGravity="fill"
                    />

        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"  />
</android.support.design.widget.CoordinatorLayout>


<!-- Listview to display slider menu -->
<ListView
    android:id="@+id/list_slidermenu"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="end"
    android:choiceMode="singleChoice"
    android:divider="@color/list_divider"
    android:dividerHeight="1dp"
    android:listSelector="@drawable/list_selector"
    android:background="@color/list_background"/>

tab_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >

    <android.support.v7.widget.CardView
        android:id="@+id/cardview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="hello"/>
    </android.support.v7.widget.CardView>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <RelativeLayout
            android:id="@+id/layoutTop"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <VideoView
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/video_player_view"
                android:layout_height="200dp"
                android:layout_width="fill_parent"/>

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/layoutBottom"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/layoutTop">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="hello"/>

        </RelativeLayout>
        <ImageView
            android:id="@+id/overlapImage"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_above="@id/layoutBottom"
            android:adjustViewBounds="true"
            android:src="@mipmap/ic_launcher" />

    </RelativeLayout>
</LinearLayout>

TabFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    final View view = inflater.inflate(R.layout.tab_fragment, container, false);
    VideoView video_player_view = (VideoView) view.findViewById(R.id.video_player_view);
    video_player_view.setVideoURI("videourl");
    video_player_view.setZOrderOnTop(true);
    video_player_view.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
             @Override
             public void onPrepared(MediaPlayer mp) {
                 video_player_view.start();
             }
         });        
    return  view;
}

那么有人可以指导我一个替代方案吗?


在绘制抽屉时,使用setZOrderOnTop(false) - Shark
如果在开场之前停止您的视频,会发生什么情况? - Initerworker
1
将代码的相关部分放入其中。DrawerLayoutVideoView - Amir
在你的tabs_fragment中,移除其他视图,只放置单个videoView。我猜其他布局出现在你的VideoView之上,你只听到声音。(确保移除setZOrderOnTop - Amir
@Atula 请尝试我的答案...... - Arjun saini
显示剩余12条评论
4个回答

5

@Atula 我为此建议一个简单的解决方案....这个方案完美地工作

我附上截图,正常运行....

试试这个

使用这个

  video_player_view.setZOrderMediaOverlay(true);

替换为

  video_view.setZOrderOnTop(true);

enter image description here


1
当您调用setZOrderOnTop(true)时,它将置于其他布局之上。实际上,它控制SurfaceView的表面是否位于其窗口之上 [查看更多]

我在我的选项卡中有一个视频,一开始看不见,但可以听到声音。

根据您的评论,您将其他布局放在了VideoView上,因此您无法看到VideoView,但是您能听到声音。

通过在手机上打开Layout bound

Setting-> developer option -> show Layout bound

您可以在您的Layouts中看到正在发生的事情。我猜您无法看到VideoView是因为您的layout_height中有那些match_parents,请仔细检查它们(例如:为每个设置50dp),您的问题将得到解决。
此外,将根布局更改为match_parent而不是wrap_content。它不能解决问题,但更好一些。

你的回答并没有直接帮助到我,但是却给了我解决问题的灵感,谢谢。 - Atula

0

你尝试过使用TextureView而不是VideoView吗?
VideoView众所周知存在缺陷。

我写了frame-video-view,其中包括简化使用TextureView作为视频播放器的功能。


0
在一些评论后,原帖作者想要看到一些代码。
好的,我们来试试这个:
private DrawerLayout drawer;
private ActionBarDrawerToggle toggle;

protected void onResume() {
    drawer.addDrawerListener(toggle);
    toggle.syncState();
}

protected void onPause() {
    drawer.removeDrawerListener(toggle);
    toggle.syncState();
}

protected void onCreate(Bundle saveInstanceState) {

    toggle = new ActionBarDrawerToggle(this, drawer, 
        toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
            public void onDrawerClosed(View view) {
                Log.d(TAG, "--> ActionBarDrawerToggle::onDrawerClosed
                    (view=" + view + ")");
                syncActionBarArrowState();
                //call videoView.setZOrderOnTop(true) so it's over everything
            }

            public void onDrawerOpened(View drawerView) {

                Log.d(TAG, "--> ActionBarDrawerToggle::onDrawerOpened
                    (drawerView=" + drawerView + ")");
                toggle.setDrawerIndicatorEnabled(true);
                //call videoView.setZOrderOnTop(false) so it's NOT over the drawer
            }

            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                 super.onDrawerSlide(drawerView, 0);
                 //call videoView.setZOrderOnTop(false) 
                 //so it's not over the drawer being pulled out
            }
        };
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接