how to force orientation mode in a particular view

本秂侑毒 提交于 2019-12-13 03:05:08

问题


Doing an cordova app with angularjs as the frontend framework. I want a particular view in a phonegap/cordova app to appear in landscape mode. The various options I've explored so far are:

  • Setting the config.xml. But you'd want this only if you intend the whole app to either be in portrait or landscape
  • Adding manifest.json. (Also taking care that I do not have any orientation settings defined in my config.xml). My plan was to conditionally include the file when I want to set my orientation as landscape.

So for the 2nd option I've written code in my pages such as:

<link rel="manifest" href="manifest.json" ng-if="orientation == 'landscape'">

...and in the controller toggling the value of orientation - with the intent of forcing it on demand.

However the above approach doesn't work.

Any idea on how to make this work?


回答1:


I don't know about the angularjs part. But you can try/use Orientationlock plugin to dynamically lock and unlock the Orientation.

From the plugin description

From your JavaScript code call window.plugins.orientationLock.unlock() to unlock orientation, window.plugins.orientationLock.lock("portrait") or window.plugins.orientationLock.lock("landscape") to lock your screen to the specified orientation.

To start your Cordova application pre-locked place setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); or setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); in the onCreate() of your Cordova activity.

Once unlocked, you can track orientation changes with the regular orientationchange event:

window.addEventListener("orientationchange", function() {
   alert(window.orientation);
}); 


来源:https://stackoverflow.com/questions/28086713/how-to-force-orientation-mode-in-a-particular-view

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