Android 设置横屏竖屏的方法

本文介绍了在Android中设置横竖屏的方法,包括通过AndroidManifest.xml设置activity的android:screenOrientation属性以及在Java代码中使用setRequestedOrientation()函数。详细讨论了如何避免在横竖屏切换时Activity被重新创建,提供了相关博客链接和官方文档参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

ConstantValueDescription
behind3Keep the screen in the same orientation as whatever is behind this activity. Corresponds toActivityInfo.SCREEN_ORIENTATION_BEHIND.
fullSensoraOrientation 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.
fullUserdRespect 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.
landscape0Would 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.
lockedeScreen is locked to its current rotation, whatever that is. Corresponds to ActivityInfo.SCREEN_ORIENTATION_LOCKED.
nosensor5Always ignore orientation determined by orientation sensor: the display will not rotate when the user moves the device. Corresponds to ActivityInfo.SCREEN_ORIENTATION_NOSENSOR.
portrait1Would 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.
reverseLandscape8Would like to have the screen in landscape orientation, turned in the opposite direction from normal landscape. Corresponds to ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE.
reversePortrait9Would like to have the screen in portrait orientation, turned in the opposite direction from normal portrait. Corresponds to ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT.
sensor4Orientation 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.
sensorLandscape6Would 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.
sensorPortrait7Would 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.
unspecifiedffffffffNo 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.
user2Use the user's current preferred orientation of the handset. Corresponds to ActivityInfo.SCREEN_ORIENTATION_USER.
userLandscapebWould 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.
userPortraitcWould 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

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值