设置 Android 屏幕方向相关

发布时间 2023-09-13 10:15:49作者: lazy12138

Activity 在AndroidManifest.xml设置方向android:screenOrientation

正向拿着手机(平时正常拿手机)对应角度为0度,此时屏幕为为竖屏,旋转180度,对应角度180度,此时屏幕为反向竖屏;
90时此时屏幕为横屏(用户右侧(正向)横屏拿着手机),屏幕角度为270,此时屏幕为反向横屏。
Android可以利用android:screenOrientation控制activity启动时方向,取值可以为:

unspecified,默认值,由系统决定,不同手机可能不一致,设置了这个属性会根据手机本身的传感器方向变化。
landscape,强制横屏显示
portrait,强制竖屏显
“reverselandscape” 与正常的横向方向相反显示。
“reverseportrait” 与正常的纵向方向相反显示。
behind,与前一个activity方向相同
“sensor” 根据设备传感器方向设置屏幕方向,当用户旋转设备时,显示的方向会改变。但默认情况下,有些设备不会在所有的四个方向上都旋转,因此要允许在所有的四个方向上都能旋转,就要使用fullsensor属性值。
“fullsensor” 显示的方向(4个方向)是由设备的方向传感器来决定的,除了它允许屏幕有4个显示方向之外,其他与设置为“sensor”时情况类似。
sensorLandscape,横屏旋转,一般横屏游戏会这样设置
sensorPortrait,竖屏旋转
nosensor,旋转设备时候,界面不会跟着旋转。初始化界面方向由系统控制
user,用户当前设置的方向

对应角度
android:screenOrientation="landscape"为90度(横屏)
android:screenOrientation="reverseLandscape"为270度(反向横屏)
android:screenOrientation="sensorLandscape"为90度和270度根据G-sensor切换(横屏切换)
android:screenOrientation="portrait"为0度(竖屏)
android:screenOrientation="reversePortrait"为180度(反向竖屏)
android:screenOrientation=“sensorPortrait”为0度和180度之间切换(竖屏切换)
转自:Android 屏幕方向相关,setRequestedOrientation,OrientationEventListener_lidongxiu0714的博客-CSDN博客