Android - Lock orientation to two directions only

て烟熏妆下的殇ゞ 提交于 2019-11-30 02:53:32

问题


How can make my android application to be Landscape or portrait but without the reverse directions?

Meaning that I want to enable SCREEN_ORIENTATION_PORTRAIT and SCREEN_ORIENTATION_LANDSCAPE but to disable SCREEN_ORIENTATION_REVERSE_LANDSCAPE and SCREEN_ORIENTATION_REVERSE_PORTRAIT.

Thanks


回答1:


I had the same problem/feature request in my camera app, and found this post:

android camera surfaceview orientation

In changeRotation just comment ORIENTATION_PORTRAIT_INVERTED and ORIENTATION_LANDSCAPE_INVERTED, and create a method rotateViews(int degrees) to rotate all of the images(buttons) of your layout when switching between regular landscape/portrait.

Also,

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

worked better for me than

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR | ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);



回答2:


  • Make sure you notify the system that you want to handle orientation changes by adding "orientation" to the android:configChanges flag attribute in the activity tag in your manifest file.

  • Override Activity.onConfigurationChanged() and inspect the Configuration.orientation field in the passed Configuration parameter and set the orientation of the activity to either ActivityInfo.SCREEN_ORIENTATION_PORTRAIT or ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE using Activity.setRequestedOrientation appropriately.



来源:https://stackoverflow.com/questions/5927486/android-lock-orientation-to-two-directions-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!