简体   繁体   English

Android:强制横向方向但允许旋转

[英]Android: Force landscape orientation but allow rotation

I want to force my Android app into lanscape orientation. 我想强制我的Android应用程序进入lanscape方向。 I managed to do that by adding screenOrientation in AndroidManifest.xml like this: 我设法通过在AndroidManifest.xml添加screenOrientation来做到这一点,如下所示:

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

or by adding the following line in the onCreate function of my only Activity : 或者在我唯一的ActivityonCreate函数中添加以下行:

setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

The problem is that both the solutions above disables rotation. 问题是上述两种解决方案都禁用了旋转。 How do I force landscape but allow rotation? 如何强制横向但允许旋转? I want to be able to rotate the phone 180 degrees. 我希望能够将手机旋转180度。 From landscape to the other landscape. 从景观到其他景观。

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

Refer to this link http://developer.android.com/reference/android/R.attr.html#screenOrientation 请参阅此链接http://developer.android.com/reference/android/R.attr.html#screenOrientation

For your activity, set the attribute as sensorLandscape 对于您的活动,将属性设置为sensorLandscape

This enables to have the screen in landscape orientation, but can use the sensor to change which direction the screen is facing. 这使得屏幕可以横向显示,但可以使用传感器来改变屏幕朝向的方向。 Corresponds to SCREEN_ORIENTATION_SENSOR_LANDSCAPE. 对应于SCREEN_ORIENTATION_SENSOR_LANDSCAPE。

Code sample 代码示例

 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM