android:screenOrientation =“锁定”旧操作系统版本

时间:2014-01-26 08:30:06

标签: android android-activity screen-orientation android-orientation

即使设备已旋转,我也希望我的活动能够保持其开始的方向,无论是纵向还是横向。 API级别18引入了应该完成工作的android:screenOrientation=”locked”Locks the orientation to its current rotation, whatever that is.

但是,我也想支持较旧的OS版本。我怎样才能做到这一点?

我知道Lock screen orientation (Android)讨论了横向模式的锁定活动,How do I disable orientation change on Android?讨论了纵向模式下的锁定活动(这在三星Galaxy S,操作系统上根本不起作用) 2.3.3)。我也知道advice recommending not to do this

1 个答案:

答案 0 :(得分:1)

MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (getResources().getConfiguration().orientation == Configuration. ORIENTATION_LANDSCAPE)
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    else
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    // ...
}

我还修改了清单文件。但是,我不确定是否需要这些更改,至少android:screenOrientation="unspecified"是默认的screenOrientation值。 的AndroidManifest.xml

<activity android:name="MainActivity"
    android:configChanges="keyboardHidden|orientation|screenSize"
    android:screenOrientation="unspecified" />