简体   繁体   中英

Android: Force landscape orientation but allow rotation

I want to force my Android app into lanscape orientation. I managed to do that by adding screenOrientation in AndroidManifest.xml like this:

  <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 :

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. 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

For your activity, set the attribute as 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.

Code sample

 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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