Android 屏幕方向行为

Android ScreenOrientation behavior

我有一个 activity,我想始终保持纵向布局。我在清单文件中获得了这些设置:

<activity android:name="Main"
            android:launchMode="singleInstance"
            android:label="@string/app_name" android:screenOrientation="portrait" android:configChanges="keyboardHidden|orientation" android:windowSoftInputMode="stateAlwaysHidden|adjustNothing">

此设置使得 activity 如果我将设备翻转到侧面,则只需将纵向布局加载到侧面。我希望布局保持在纵向位置,仅此而已,即使这意味着在将设备切换 90 度时在侧面查看布局。

从Android3.2开始,还需要在configChanges

中加上screenSize
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation|screenSize"

来自 handling runtime changes

的开发者文档

Caution: Beginning with Android 3.2 (API level 13), the "screen size" also changes when the device switches between portrait and landscape orientation.

动态强制:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);