如何将应用程序限制为仅适用于所有平台的离子纵向模式?

How to restrict app to portrait mode only in ionic for all platforms?

我正在研究 Ionic/Cordova,我已将其添加到 androidManifest.xml,但这对我不起作用,应用程序仍以两种方式显示

android:screenOrientation="portrait"

如何将我的应用限制为仅纵向模式?我检查了 android 但它不起作用

如果您想在所有设备上限制为纵向模式,您应该将此行添加到项目根文件夹中的 config.xml。

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

之后,请在命令行中输入以下文本重建平台:

ionic build

根据https://cordova.apache.org/docs/en/4.0.0/config_ref/#global-preferences

Orientation allows you to lock orientation and prevent the interface from rotating in response to changes in orientation. Possible values are default, landscape, or portrait. Example:

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

请注意,这些值区分大小写,它是 "Orientation",而不是 "orientation"。

在您的 angular app.js 中添加行 screen.lockOrientation('portrait'); 如下所示:

一个

angular.module('app', ['ionic'])
.run(function($ionicPlatform) {
    // LOCK ORIENTATION TO PORTRAIT.
    screen.lockOrientation('portrait');
  });
})

你在.run函数中还会有其他代码,但你感兴趣的是我写的那一行。 我没有在我的代码中添加行 <preference name="orientation" value="portrait" />,但您可能需要添加 cordova-plugin-device-orientation

首先,您需要使用

添加Cordova Screen Orientation Plugin
cordova plugin add cordova-plugin-screen-orientation

然后将screen.lockOrientation('portrait');添加到.run()方法

angular.module('myApp', [])
.run(function($ionicPlatform) {

    screen.lockOrientation('portrait');
    console.log('Orientation is ' + screen.orientation);

});
})

你可以试试这个:-

Cordova 插件 set/lock 以 iOS、Android、WP8 和 Blackberry 10 的通用方式显示屏幕方向。此插件基于早期版本的屏幕方向 API 所以 api 当前与当前规范不匹配。

https://github.com/apache/cordova-plugin-screen-orientation

或在 xml 配置中尝试此代码:-

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

Ionic v2+ 更新

对于 Ionic 版本 2 及更高版本,您还需要为您使用的任何插件安装相应的 Ionic Native 包,包括 cordova-plugin-phonegap-plugin- 插件。

  1. 安装 Ionic Native 插件

    为来自 CLI 的插件安装 Ionic Native 的 TypeScript 模块。

    $ ionic cordova plugin add --save cordova-plugin-screen-orientation
    $ npm install --save @ionic-native/screen-orientation
    

    * 请注意,--save 命令是可选的,如果您不想将插件添加到 package.json 文件中,则可以省略该命令

  2. 导入ScreenOrientation插件

    将插件导入您的 controllerdocumentation 中提供了更多详细信息。

    import { ScreenOrientation } from '@ionic-native/screen-orientation';
    
    @Component({
        templateUrl: 'app.html',
        providers: [
            ScreenOrientation
        ]
    })
    
    constructor(private screenOrientation: ScreenOrientation) {
      // Your code here...
    }
    
  3. 做你的事

    以编程方式锁定和解锁屏幕方向。

    // Set orientation to portrait
    this.screenOrientation.lock(this.screenOrientation.ORIENTATIONS.PORTRAIT);
    
    // Disable orientation lock
    this.screenOrientation.unlock();
    
  4. 奖励积分

    您还可以获得当前方向。

    // Get current orientation
    console.log(this.screenOrientation.type);  // Will return landscape" or "portrait"
    

如果你想在你的方向上做一些事情意味着你想在改变你的应用程序方向时执行任何操作,那么你必须使用离子框架插件...... https://ionicframework.com/docs/native/screen-orientation/

如果您只想将您的应用限制为纵向或横向模式,那么您只需在 config.xml

中添加以下行
<preference name="orientation" value="portrait" />
                 OR
<preference name="orientation" value="landscape" />

在config.xml中添加以下行

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

请在 androidManifest.xml 文件中添加 android:screenOrientation="portrait" 作为 android 的 activity 标签的属性。

<activity
        android:name="com.example.demo_spinner.MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait" >
</activity>