首页 文章

VideoView方向更改为全屏(显示/隐藏操作栏和通知)

提问于
浏览
0

嗨,我在播放方向更改时遇到此问题 . 在肖像上,我的布局目录中有以下XML .

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <!-- Player Header -->
    <LinearLayout 
        android:id="@+id/player_header_bg"
        android:layout_width="fill_parent"
        android:layout_height="60dip"
        android:background="@layout/bg_player_header"
        android:layout_alignParentTop="true"
        android:paddingLeft="5dp"
        android:paddingRight="5dp">

        <!-- Song Title -->
        <TextView 
            android:id="@+id/videoTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textColor="#04b3d2"
            android:textSize="16sp"
            android:paddingLeft="10dp"
            android:textStyle="bold"
            android:text="@string/song_def_title"
            android:layout_marginTop="10dp"/>

        <!-- Full Screen button -->
        <ImageButton 
            android:id="@+id/btnFullScreen"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:src="@drawable/ic_action_refresh"
            android:background="@null" />

        <!-- Playlist button -->
        <ImageButton 
            android:id="@+id/btnPlaylist"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:src="@drawable/btn_playlist"
            android:background="@null"/>
    </LinearLayout>

    <VideoView
        android:id="@+id/surface_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/player_header_bg" >

    </VideoView>

</RelativeLayout>

当方向更改为横向时,我在layout-land文件夹中具有此XML,该文件夹具有相同的XML文件名但具有以下XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <VideoView
        android:id="@+id/surface_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" >
    </VideoView>

</RelativeLayout>

方向更改没有任何问题,但我想在横向模式下获取操作栏和通知栏 . 并以纵向模式出现 .

我试图手动覆盖onConfigurationChanged方法但它运行不正常 .

@Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            ActionBar actionBar = getActionBar();
            actionBar.hide();

            WindowManager.LayoutParams attrs = getWindow().getAttributes(); 
            attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN; 
            getWindow().setAttributes(attrs); 

            // setContentView(R.layout.player_video_fs);
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            ActionBar actionBar = getActionBar();
            actionBar.show();

            WindowManager.LayoutParams attrs = getWindow().getAttributes(); 
            attrs.flags |= WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN; 
            getWindow().setAttributes(attrs); 

            // setContentView(R.layout.player_video);
        }
    }

如果我要执行上述操作,当我更改为横向时,它将删除操作栏和通知栏,但当我更改回纵向时,操作栏会变得有些奇怪 . 看看它是如何被切断的?动作栏

actiobar

并且改为横向,看起来它是黑屏而不是继续播放视频 .

我尝试过其他一些方法,但仍然无法解决 . 任何的想法?

1 回答

  • 1

    我也遇到了这个问题,偶然发现了这个问题 . 我知道它已经过时了,但是为了帮助未来的开发人员来到这里 .

    在搜索之后我找到了一个由Praveen提供的简单解决方案,而不是使用复合或运算符(| =)来设置窗口属性标志,你可以简单地调用 getWindow().clearFlags(flag) or getWindow().addFlags(flag) 函数

    因此,我在横向中隐藏动作栏的修订版本如下所示:

    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        getActionBar().hide();
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
        getActionBar().show();
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
    

相关问题