如何在本机脚本中设置方向 [英] How to set orientation in nativescript

查看:83
本文介绍了如何在本机脚本中设置方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想知道如何在nativescript中设置设备方向. 具体来说,我希望所编写的应用程序始终保持相同的方向(人像),以免旋转设备不会导致其进入横向.

Hello I would like to know how to set device orientation in nativescript. Specifically I want the application that I am writing to stay in the same orientation (portrait) at all times so that rotating the device does not cause it to go into landscape.

我尝试了nativescript-orientation插件和setOrientation.

I tried the nativescript-orientation plugin and setOrientation.

var orientation = require('nativescript-orientation');
console.log(JSON.stringify(orientation));// outputs JS: {}
orientation.setOrientation("portrait"); 

但是我收到错误消息无法读取未定义的属性setOrientation. tns插件列表显示该插件已安装.我也尝试删除platforms/android目录并以相同的结果运行tns platform add android.

However I get the error "Cannot read property setOrientation of undefined. tns plugin list shows that the plugin is installed. Also I tried removing the platforms/android directory and running tns platform add android with the same result.

我还尝试将android:screenOrientation="portrait"的各种组合放入AndroidManifest.xml中,但没有成功.

I also tried putting various combinations of android:screenOrientation="portrait" into AndroidManifest.xml without success.

AndroidManifest.xml看起来像这样

AndroidManifest.xml from inside App_resources looks like this

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="__PACKAGE__"
    android:versionCode="1"
    android:versionName="1.0">

    <supports-screens
        android:smallScreens="true"
        android:normalScreens="true"
        android:largeScreens="true"
        android:xlargeScreens="true"/>

    <uses-sdk
        android:minSdkVersion="17"
        android:targetSdkVersion="__APILEVEL__"/>

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:screenOrientation="portrait"
        android:name="com.tns.NativeScriptApplication"
        android:allowBackup="true"
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">

        <activity
            android:name="com.tns.NativeScriptActivity"
            android:label="@string/title_activity_kimera"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:theme="@style/LaunchScreenTheme">
            <meta-data android:name="SET_THEME_ON_LAUNCH" android:resource="@style/AppTheme" />

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.tns.ErrorReportActivity"/>
    </application>
</manifest>

推荐答案

您将不得不更新您的AndroidManifest.xml& App_Resources中的Info.plist.

You will have to update your AndroidManifest.xml & Info.plist in your App_Resources.

AndroidManifest.xml

在主要活动中将screenOrientation设置为纵向

Set screenOrientation to portrait on your main activity

<activity android:name="com.tns.NativeScriptActivity" 
 ... 
 android:screenOrientation="portrait">

Info.plist

仅保持纵向,将其余部分从UISupportedInterfaceOrientations中移除.

Keep only the portrait orientation, remove rest from UISupportedInterfaceOrientations.

<key>UISupportedInterfaceOrientations</key>
<array>
  <string>UIInterfaceOrientationPortrait</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
  <string>UIInterfaceOrientationPortrait</string>
  <string>UIInterfaceOrientationPortraitUpsideDown</string>
</array>

注意:请确保在进行这些更改后运行干净的版本.

Note: Make sure you run a clean build after these changes.

这篇关于如何在本机脚本中设置方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆