Android:强制横向但允许旋转

Android: Force landscape orientation but allow rotation

我想强制我的 Android 应用程序进入横向。我设法通过在 AndroidManifest.xml 中添加 screenOrientation 来做到这一点,如下所示:

  <activity android:name="SomeActivity"
              android:label="@string/app_name"
              android:screenOrientation="landscape">

或者在我唯一的 ActivityonCreate 函数中添加以下行:

setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

问题是以上两种解决方案都禁用了旋转。如何强制横向但允许旋转?我希望能够将 phone 旋转 180 度。从一道风景到另一道风景。

您只需将 AndroidManifest.xml 中的 android:screenOrientation 属性设置为 sensorLandscape

参考这个linkhttp://developer.android.com/reference/android/R.attr.html#screenOrientation

对于您的 activity,将属性设置为 sensorLandscape

这使屏幕可以横向显示,但可以使用传感器更改屏幕朝向的方向。对应于SCREEN_ORIENTATION_SENSOR_LANDSCAPE.

代码示例

 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);