Android 设置横屏竖屏的方法

1.AndroidManifest.xml设置activity标签中的android:screenOrientation属性

android Studio官方网站的说明:

android:screenOrientation

Activity 在设备上的显示方向。如果 Activity 是在多窗口模式下运行,系统会忽略该属性。

其值可以是下列任一字符串:

unspecified 默认值。由系统选择方向。在不同设备上,系统使用的政策以及基于政策在特定上下文所做的选择可能有所差异。
behind 与 Activity 栈中紧接着它的 Activity 的方向相同。
landscape 横向方向(显示的宽度大于高度)。
portrait 纵向方向(显示的高度大于宽度)。
reverseLandscape 与正常横向方向相反的横向方向。API 级别 9 中的新增配置。
reversePortrait 与正常纵向方向相反的纵向方向。API 级别 9 中的新增配置。
sensorLandscape 横向方向,但根据设备传感器,可以是正常或反向的横向方向。API 级别 9 中的新增配置。
sensorPortrait 纵向方向,但根据设备传感器,可以是正常或反向的纵向方向。API 级别 9 中的新增配置。
userLandscape 横向方向,但根据设备传感器和用户的传感器首选项,可以是正常或反向的横向方向。 如果用户锁定了基于传感器的旋转,其行为与 landscape 相同,否则,其行为与 sensorLandscape 相同。API 级别 18 中的新增配置。
userPortrait 纵向方向,但根据设备传感器和用户的传感器首选项,可以是正常或反向的纵向方向。 如果用户锁定了基于传感器的旋转,其行为与 portrait 相同,否则,其行为与 sensorPortrait 相同。API 级别 18 中的新增配置。
sensor 方向由设备方向传感器决定。显示方向取决于用户如何手持设备,它会在用户旋转设备时发生变化。 但一些设备默认情况下不会旋转到所有四种可能的方向。要允许全部四种方向,请使用 "fullSensor"
fullSensor 方向由 4 种方向中任一方向的设备方向传感器决定。这与 "sensor" 类似,不同的是它允许所有 4 种可能的屏幕方向,无论设备正常情况下采用什么方向(例如,一些设备正常情况下不使用反向纵向或反向横向,但它支持这些方向)。 API 级别 9 中的新增配置。
nosensor 决定方向时不考虑物理方向传感器。传感器会被忽略,因此显示不会随用户对设备的移动而旋转。 除了这个区别,系统在选择方向时使用的政策与“unspecified”设置相同。
user 用户当前的首选方向。
"fullUser" 如果用户锁定了基于传感器的旋转,其行为与 user 相同,否则,其行为与 fullSensor 相同,允许所有 4 种可能的屏幕方向。 API 级别 18 中的新增配置。
locked 将方向锁定在其当前的任意旋转方向。API 级别 18 中的新增配置。

2.在Java代码中setRequestedOrientation

横屏:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

竖屏:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

官方说明:

screenOrientation

added in API level 1

 

public static final int screenOrientation

Specify the orientation an activity should be run in. If not specified, it will run in the current preferred orientation of the screen.

This attribute is supported by the <activity> element.

Must be one of the following constant values.

Constant Value Description
behind 3 Keep the screen in the same orientation as whatever is behind this activity. Corresponds toActivityInfo.SCREEN_ORIENTATION_BEHIND.
fullSensor a Orientation is determined by a physical orientation sensor: the display will rotate based on how the user moves the device. This allows any of the 4 possible rotations, regardless of what the device will normally do (for example some devices won't normally use 180 degree rotation). Corresponds to ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR.
fullUser d Respect the user's sensor-based rotation preference, but if sensor-based rotation is enabled then allow the screen to rotate in all 4 possible directions regardless of what the device will normally do (for example some devices won't normally use 180 degree rotation). Corresponds to ActivityInfo.SCREEN_ORIENTATION_FULL_USER.
landscape 0 Would like to have the screen in a landscape orientation: that is, with the display wider than it is tall, ignoring sensor data. Corresponds to ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE.
locked e Screen is locked to its current rotation, whatever that is. Corresponds to ActivityInfo.SCREEN_ORIENTATION_LOCKED.
nosensor 5 Always ignore orientation determined by orientation sensor: the display will not rotate when the user moves the device. Corresponds to ActivityInfo.SCREEN_ORIENTATION_NOSENSOR.
portrait 1 Would like to have the screen in a portrait orientation: that is, with the display taller than it is wide, ignoring sensor data. Corresponds to ActivityInfo.SCREEN_ORIENTATION_PORTRAIT.
reverseLandscape 8 Would like to have the screen in landscape orientation, turned in the opposite direction from normal landscape. Corresponds to ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE.
reversePortrait 9 Would like to have the screen in portrait orientation, turned in the opposite direction from normal portrait. Corresponds to ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT.
sensor 4 Orientation is determined by a physical orientation sensor: the display will rotate based on how the user moves the device. Ignores user's setting to turn off sensor-based rotation. Corresponds toActivityInfo.SCREEN_ORIENTATION_SENSOR.
sensorLandscape 6 Would like to have the screen in landscape orientation, but can use the sensor to change which direction the screen is facing. Corresponds to ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE.
sensorPortrait 7 Would like to have the screen in portrait orientation, but can use the sensor to change which direction the screen is facing. Corresponds to ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT.
unspecified ffffffff No preference specified: let the system decide the best orientation. This will either be the orientation selected by the activity below, or the user's preferred orientation if this activity is the bottom of a task. If the user explicitly turned off sensor based orientation through settings sensor based device rotation will be ignored. If not by default sensor based orientation will be taken into account and the orientation will changed based on how the user rotates the device. Corresponds to ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED.
user 2 Use the user's current preferred orientation of the handset. Corresponds to ActivityInfo.SCREEN_ORIENTATION_USER.
userLandscape b Would like to have the screen in landscape orientation, but if the user has enabled sensor-based rotation then we can use the sensor to change which direction the screen is facing. Corresponds toActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE.
userPortrait c Would like to have the screen in portrait orientation, but if the user has enabled sensor-based rotation then we can use the sensor to change which direction the screen is facing. Corresponds toActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT.

Constant Value: 16842782 (0x0101001e)

_______________________________________分割线__________________________________________

Android 设置横屏禁止Activity重新创建(看我的另一篇博客)

https://blog.csdn.net/yh18668197127/article/details/85120811

参考

Android Studio官方<activity>标签说明

https://developer.android.com/guide/topics/manifest/activity-element?hl=zh-cn#config

猜你喜欢

转载自blog.csdn.net/yh18668197127/article/details/85119856