android - 如何在 nativescript 中设置方向

标签 android screen-orientation nativescript

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

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

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

但是我收到错误“无法读取未定义的属性 setOrientation。 tns plugin list 显示插件已安装。我还尝试删除 platforms/android 目录并运行 tns platform add android 得到相同的结果。

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

App_resources 中的 AndroidManifest.xml 看起来像这样

<?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>

最佳答案

您必须更新 App_Resources 中的 AndroidManifest.xmlInfo.plist

AndroidManifest.xml

在您的主要 Activity 中将 screenOrientation 设置为纵向

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

信息.plist

只保留纵向,从 UISupportedInterfaceOrientations 中移除其余部分。

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

注意:确保在这些更改后运行干净的构建。

关于android - 如何在 nativescript 中设置方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40410561/

相关文章:

安卓模拟器与手机不匹配

android - 蜂窝模拟器纵向翻转屏幕

javascript - 我该如何解决 dev webpack style hot loader js 错误

nativescript - 类型错误 : Cannot read property 'cartesianChart' of undefined in nativescript

Android 4.2 破坏了我的 AES 加密/解密代码

Android + coreLibraryDesugaring : which Java 11 APIs can I expect to work?

android - 如何从android内存中选择一个文件对其进行一些操作

css - 如何让滚动只在一个 <div> 上起作用?

android - 防止 Activity 重新创建我的方向变化 fragment

android - 寻找一种允许客户端查看 NativeScript 应用程序项目实时更新的方法