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

2017-04-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);
        }
    }
上一篇下一篇

猜你喜欢

热点阅读