如何在 Android 中禁用横向模式?
如何为我的 Android 应用中的某些视图禁用横向模式?
How can I disable landscape mode for some of the views in my Android app?
将 android:screenOrientation="portrait"
添加到 AndroidManifest.xml 中的 Activity.例如:
Add android:screenOrientation="portrait"
to the activity in the AndroidManifest.xml. For example:
<activity android:name=".SomeActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" />
既然这已经成为一个超级流行的答案,我感到非常内疚,因为强制肖像很少是它经常应用的问题的正确解决方案.
强制肖像的主要警告:
Since this has become a super-popular answer, I feel very guilty as forcing portrait is rarely the right solution to the problems it's frequently applied to.
The major caveats with forced portrait:
- 这并不能免除您必须考虑活动生命周期事件或正确保存/恢复状态.有很多除了可以触发活动的应用程序轮换之外的事情破坏/娱乐,包括多任务处理等不可避免的事情.没有捷径;学习使用 bundle 和
retainInstance
片段. - 请记住,与相当统一的 iPhone 体验不同,有些设备的竖向并不是明显流行的方向.当用户使用带有硬件键盘或游戏手柄的设备(如 Nvidia Shield)时,在 Chromebook 上,关于 折叠,或在 Samsung DeX 上,强制肖像可以使您的应用程序遇到限制或巨大的可用性麻烦.如果您的应用程序没有强大的 UX 参数会导致支持其他方向的负面体验,那么您可能不应该强制横向.我说的是诸如这是一个收银机应用程序,适用于始终在固定硬件基座中使用的一种特定型号的平板电脑."
- This does not absolve you of having to think about activity
lifecycle events or properly saving/restoring state. There are plenty of
things besides app rotation that can trigger an activity
destruction/recreation, including unavoidable things like multitasking. There are no shortcuts; learn to use bundles and
retainInstance
fragments. - Keep in mind that unlike the fairly uniform iPhone experience, there are some devices where portrait is not the clearly popular orientation. When users are on devices with hardware keyboards or game pads a la the Nvidia Shield, on Chromebooks, on foldables, or on Samsung DeX, forcing portrait can make your app experience either limiting or a giant usability hassle. If your app doesn't have a strong UX argument that would lead to a negative experience for supporting other orientations, you should probably not force landscape. I'm talking about things like "this is a cash register app for one specific model of tablet always used in a fixed hardware dock."
因此,大多数应用应该让手机传感器、软件和物理配置自行决定用户希望如何与您的应用进行交互.不过,如果您对用例中 sensor
方向的默认行为不满意,您可能仍需要考虑以下几种情况:
So most apps should just let the phone sensors, software, and physical configuration make their own decision about how the user wants to interact with your app. A few cases you may still want to think about, though, if you're not happy with the default behavior of sensor
orientation in your use case:
- 如果您主要担心的是在活动过程中意外的方向变化,而您认为设备的传感器和软件无法很好地应对(例如,在基于倾斜的游戏中),请考虑支持横向和纵向,但使用
nosensor
用于方向.这迫使大多数平板电脑上的风景和大多数手机上的肖像,但我仍然不会推荐大多数正常"的人使用它.应用(有些用户只是喜欢在手机上横向软键盘输入,而许多平板电脑用户喜欢纵向阅读——你应该让他们这样做). - 如果您仍然出于某种原因需要强制使用肖像,
sensorPortrait
可能比portrait
对于 Android 2.3 (Gingerbread) 及更高版本;这允许颠倒肖像,这在平板电脑使用中很常见.
- If your main concern is accidental orientation changes mid-activity that you think the device's sensors and software won't cope with well (for example, in a tilt-based game) consider supporting landscape and portrait, but using
nosensor
for the orientation. This forces landscape on most tablets and portrait on most phones, but I still wouldn't recommend this for most "normal" apps (some users just like to type in the landscape softkeyboard on their phones, and many tablet users read in portrait - and you should let them). - If you still need to force portrait for some reason,
sensorPortrait
may be better thanportrait
for Android 2.3 (Gingerbread) and later; this allows for upside-down portrait, which is quite common in tablet usage.