清单文件上:
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);
}
}
网友评论