离子2:屏幕方向像android中的肖像和风景

时间:2017-06-28 05:31:45

标签: ionic-framework ionic2

当我在纵向和横向两个方向旋转屏幕但是离子2的android应用程序不旋转

config.xml中

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

app.component.js

import { ScreenOrientation } from '@ionic-native/screen-orientation';

initializeApp() {
this.platform.ready().then(() => { 
if (this.platform.isPortrait) { this.screenOrientation.lock(this.screenOrientation.ORIENTATIONS.LANDSCAPE); } else {  this.screenOrientation.lock(this.screenOrientation.ORIENTATIONS.PORTRAIT); }
    this.statusBar.styleDefault();
    this.initPushNotification();  
}); 
}

2 个答案:

答案 0 :(得分:2)

首先安装..

离子cordova插件添加cordova-plugin-screen-orientation

npm install --save @ ionic-native / screen-orientation

app.component.ts

 import { ScreenOrientation } from '@ionic-native/screen-orientation';

 constructor(private screenOrientation: ScreenOrientation) { }

 // set to landscape

this.screenOrientation.lock(this.screenOrientation.ORIENTATIONS.LANDSCAPE);

// allow user rotate

this.screenOrientation.unlock();





  or 

   constructor(private screenOrientation: ScreenOrientation) {
    this.screenOrientation.lock('portrait');
   }

答案 1 :(得分:1)

我找到了这个问题的答案,当手机屏幕方向更改应用程序也随着手机方向旋转。

首先,从confix.xml中删除方向首选项

然后这段代码写在app.component.js

app.component.js

if (this.platform.is('android')) {        
    this.ScreenOrientation.onChange().subscribe(() => {

      if (this.platform.isPortrait) {
        this.ScreenOrientation.unlock()
      }
      else {            
        this.ScreenOrientation.lock(this.ScreenOrientation.ORIENTATIONS.PORTRAIT_PRIMARY)
      }
    });
  }