如何在phonegap中使用插件来改变屏幕方向?

Ren*_* Sá 2 iphone android screen-orientation cordova

我的phonegap应用应该以纵向方式运行.只能在横向模式下运行一个页面(index.html).然后我找到了这个插件,但对我不起作用.

cordova plugin add net.yoik.cordova.plugins.screenorientation

在config.xml中:

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

index.html的:

<html>
<head>
<title>App</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<script>
var so = cordova.plugins.screenorientation;
so.setOrientation(so.Orientation.LANDSCAPE);
</script>
Run Code Online (Sandbox Code Playgroud)

对于android,应用程序返回错误App has stopped.

对于iPhone,没有任何事情发生.

gbe*_*ben 5

看起来您正在使用v1.0之前存在的旧API.也许你在一篇旧博客文章中找到了这个例子?

无论如何,请参阅插件文档.使用当前的API看起来像:(必须在设备准备好之后)

<script>
    document.addEventListener("deviceready", onDeviceReady, false);
    function onDeviceReady()
    {
        screen.lockOrientation('landscape');
    }
</script>
Run Code Online (Sandbox Code Playgroud)