AndroidManifest.xml中对应activity添加以下属性:
android:configChanges="orientation|keyboardHidden|screenSize"在Activity中设置横竖屏:设置横屏:setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);设置竖屏:setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);在Activity的 onConfigurationChanged 方法中:
@Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { //横屏 //设置全屏即隐藏状态栏 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { //恢复状态栏 WindowManager.LayoutParams attrs = getWindow().getAttributes(); attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN); getWindow().setAttributes(attrs); getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); } }
Activity横竖屏切换
最新推荐文章于 2024-03-30 18:45:35 发布