美文网首页
全屏、小屏、横屏、竖屏的切换

全屏、小屏、横屏、竖屏的切换

作者: 仁昌居士 | 来源:发表于2017-04-25 15:25 被阅读0次

    清单文件上:
    android:configChanges="screenSize|keyboardHidden|orientation" 防止切换屏时生命周期上重新被调用
    android:screenOrientation="portrait" 屏幕固定方向

    android:name=".activity.hkplaygrid.HKPlayGridActivity"
                android:configChanges="screenSize|keyboardHidden|orientation"
                android:screenOrientation="portrait"
                android:theme="@style/AppTheme.NoActionBar"/>
    

    代码上可以直接调用方法:

     //window切换成全屏
        private void setWindowFullSrceen() {
          /*  getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置全屏*/
    
            WindowManager.LayoutParams attrs = getWindow().getAttributes();
            attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
            getWindow().setAttributes(attrs);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
    
            /**
             * 设置为横屏
             */
            if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            }
        }
    
        //window切换回小屏
        private void setWindowShrinkSrceen() {
            WindowManager.LayoutParams attrs = getWindow().getAttributes();
            attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
            getWindow().setAttributes(attrs);
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
    
            /**
             * 设置为竖屏
             */
            if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            }
        }
    

    相关文章

      网友评论

          本文标题:全屏、小屏、横屏、竖屏的切换

          本文链接:https://www.haomeiwen.com/subject/mgpvzttx.html