简体   繁体   English

cordova插件仅允许一个屏幕横向显示?

[英]cordova plugin to allow one screen landscape only?

I've tried several Cordova plugins to achieve this but none seem to work anymore. 我已经尝试了几个Cordova插件来实现此目的,但似乎都无法正常工作了。 Are there any working plugins Android 6.0 that will allow me to set both landscape/portrait (auto) on my index page then landscape only on all other pages within my app? 是否有任何可使用的Android 6.0插件,可让我在索引页面上同时设置横向/纵向(自动),然后仅在应用内的所有其他页面上横向?

I've tried about 4 plugins, following documentation to the tee and nothing. 我试过约4个插件,遵循发球台上的文档,一无所获。

You just need to set the preference in your config.xml . 您只需要在config.xml中设置首选项。

Just write this line to set phone orientation to landscape only. 只需编写此行即可将手机方向设置为仅横向

<preference name="orientation" value="landscape" />

For just only one screen your have set the orientation programmatically when screen loads with the help of this plugin . 对于仅一个屏幕,借助此插件,您可以在屏幕加载时以编程方式设置方向。

For that particular screen you can call this init function onload like this in HTML . 对于该特定屏幕,您可以在HTML中像这样调用init函数onload。

<body onload="init()">

Or you can call the jquery Document.ready(). 或者,您可以调用jquery Document.ready()。

$(document).ready(function() {
   init();
 });

Init function is just to add event listener "deviceready" which will fire only on the device. 初始化功能只是添加仅在设备上触发的事件监听器“ deviceready”。 After that you can use the Device API. 之后,您可以使用设备API。

function init(){
    document.addEventListener("deviceready", onDeviceReady, false);
}

function onDeviceReady() {
   // set to landscape
   screen.lockOrientation('landscape');
}

Then when you move out from that particular screen you can set jquery unload method like this. 然后,当您从该特定屏幕移出时,可以设置jquery卸载方法,如下所示。

 $(window).on('beforeunload', function(){
     // allow user rotate
     screen.unlockOrientation(); 
     // or set the Orientation to portrait
     screen.lockOrientation('portrait');
 });

Ok, here's what did it. 好的,这就是它的作用。 AJs solution worked but I had to ensure a few things first... AJs解决方案有效,但是我必须首先确保几件事...

  • cordova.js is linked to the index.html (or w/e page you're using) cordova.js链接到index.html(或您正在使用的w / e页面)

  • I also needed to add a link to the plugin js file: /plugins/cordova-plugin-screen-orientation/www/screenori‌​entation.js 我还需要添加一个指向插件js文件的链接:/plugins/cordova-plugin-screen-orientation/www/screenori‌entation.js

  • Then I needed to add the plugin to the config (this can be done when installing the plugin by using --save 然后我需要将插件添加到配置中(这可以通过使用--save安装插件来完成。

    • Followed AJ's instructions above, which I've marked as the solution. 遵循上面AJ的指示,我将其标记为解决方案。

One last thing, the updated unload option he provided still didn't work, so on the page I want unlocked, I used his solution just added the unlock option example: 最后一件事,他提供的更新的卸载选项仍然不起作用,因此在我要解锁的页面上,我使用他的解决方案只是添加了解锁选项示例:

$(function() {
init();
});

function init(){
document.addEventListener("deviceready", onDeviceReady, false);
}
function onDeviceReady() {
screen.unlockOrientation();
}

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

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