首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Ionic 2: android中的屏幕方向,如肖像和横向

Ionic 2: android中的屏幕方向,如肖像和横向
EN

Stack Overflow用户
提问于 2017-06-28 13:31:45
回答 2查看 3.5K关注 0票数 0

当我在纵向和横向两个方向上旋转屏幕时,ionic 2的android应用程序没有旋转

config.xml

代码语言:javascript
运行
复制
<preference name='orientation' value='portrait'/>

app.component.js

代码语言:javascript
运行
复制
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();  
}); 
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-06-28 14:43:12

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

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

然后用app.component.js编写以下代码

app.component.js

代码语言:javascript
运行
复制
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)
      }
    });
  }
票数 1
EN

Stack Overflow用户

发布于 2017-06-28 16:20:41

首先安装..

ionic cordova插件添加cordova- plugin -screen-orientation

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

app.component.ts

代码语言:javascript
运行
复制
 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');
   }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44794311

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档