簡體   English   中英

如何使用phonegap中的插件更改屏幕方向?

[英]How use plugin in phonegap for change screen orientation?

我的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>

對於android,應用程序返回錯誤App has stopped

對於iPhone,沒有任何反應。

您似乎正在使用v1.0之前的舊API。 也許您是在舊博客文章中找到了該示例?

無論如何,請參閱插件文檔 使用當前的API,它將看起來像:(必須在設備准備就緒之后)

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

首先添加此腳本

<script type="text/javascript" src="cordova.js"></script>

然后在另一個腳本標簽內

<script>
    document.addEventListener("deviceready", onDeviceReady, false);
    function onDeviceReady()
    {

       var so = cordova.plugins.screenorientation;
       so.setOrientation(so.Orientation.LANDSCAPE);
    }
</script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM