解决自动旋转和点击按钮旋转冲突问题(点击按钮旋转回退竖屏后,不能自动旋转)
答:其实只要在点击按钮旋转回竖屏时再加一句代码就行了
( activity.requestedOrientation=ActivityInfo.SCREEN_ORIENTATION_USER)
//按钮点击事件
img_fullScreen.setOnClickListener {
//横屏的时候
if (resources.configuration.orientation==Configuration.ORIENTATION_LANDSCAPE){
//变成竖屏
activity.requestedOrientation=ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
activity.requestedOrientation=ActivityInfo.SCREEN_ORIENTATION_USER
}else{
activity.requestedOrientation=ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
}
}
}
//initWidth和initHeight是竖屏时当前布局的宽和高
override fun onConfigurationChanged(newConfig:Configuration) {
isFullScreen = newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE
if (!isFullScreen){
layoutParams.width=initWidth
layoutParams.height=initHeight
activity.window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
}else{
layoutParams.width = LayoutParams.MATCH_PARENT
layoutParams.height =LayoutParams.MATCH_PARENT
activity.window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
}
super.onConfigurationChanged(newConfig)
}
网友评论