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

Muh*_*han 114 ionic-framework

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

android:screenOrientation="portrait"
Run Code Online (Sandbox Code Playgroud)

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

Vad*_*ens 304

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

<preference name="orientation" value="portrait" />
Run Code Online (Sandbox Code Playgroud)

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

ionic build
Run Code Online (Sandbox Code Playgroud)

  • 当你在真正的设备上运行它时,这似乎只能工作.如果您通过Ionic View云应用程序运行它,它将无效. (4认同)
  • 很高兴看到.为我工作.检查android.谢谢Vadiem (2认同)

Tyl*_*ler 34

Ionic v2 +更新

对于2和更高版本的离子,你还需要安装相应的离子本地包为你使用任何插件,包括cordova-plugin-phonegap-plugin-插件.

  1. 安装Ionic Native Plugin

    CLI为插件安装Ionic Native的TypeScript模块.

    $ ionic cordova plugin add --save cordova-plugin-screen-orientation
    $ npm install --save @ionic-native/screen-orientation
    
    Run Code Online (Sandbox Code Playgroud)

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

  2. 导入ScreenOrientation插件

    将插件导入到文档中controller提供的更多详细信息中.

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

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

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

    您还可以获得当前的方向.

    // Get current orientation
    console.log(this.screenOrientation.type);  // Will return landscape" or "portrait"
    
    Run Code Online (Sandbox Code Playgroud)

  • `ionic plugin`似乎不再起作用.我认为它现在是`离子cordova插件'.如`离子cordova插件添加--save cordova-plugin-screen-orientation` (2认同)
  • 添加 `&lt;preference name="Orientation" value="portrait" /&gt;` 对我来说似乎就足够了 (2认同)
  • DevApp 只是将您的应用程序作为 DevApp 应用程序中的 web 视图运行,因此某些本机功能不起作用。尝试直接在设备上编译和运行您的应用程序,它应该可以工作。 (2认同)

小智 11

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

方向允许您锁定方向并防止界面响应方向的变化而旋转.可能的值包括默认值,横向或纵向.例:

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

请注意,这些值区分大小写,它是"方向",而不是"方向".

  • "方向"对我有用.在实际设备上运行应用程序时,"Orientation"会为我提供白屏. (2认同)
  • "orientation"适用于Android设备中的Ionic 2. (2认同)

小智 6

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

如果您只想将应用程序限制为纵向或横向模式,那么您只需在 config.xml 中添加以下行

<preference name="orientation" value="portrait" />
                 OR
<preference name="orientation" value="landscape" />
Run Code Online (Sandbox Code Playgroud)


Ayu*_*yar 6

android:screenOrientation="portrait"请在文件中添加androidManifest.xml为 android 活动标记的属性。

<activity
        android:name="com.example.demo_spinner.MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait" >
</activity>
Run Code Online (Sandbox Code Playgroud)