【发布时间】:2017-03-17 13:44:29
【问题描述】:
您好,我想知道如何在 nativescript 中设置设备方向。 具体来说,我希望我正在编写的应用程序始终保持相同的方向(纵向),以便旋转设备不会导致它进入横向。
我尝试了 nativescript-orientation 插件和 setOrientation。
var orientation = require('nativescript-orientation');
console.log(JSON.stringify(orientation));// outputs JS: {}
orientation.setOrientation("portrait");
但是我收到错误“无法读取未定义的属性 setOrientation。
tns 插件列表显示插件已安装。我还尝试删除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>
【问题讨论】:
-
在修改了AndroidManifest.xml之后你做了完整的
build吗?此外,您可能需要卸载设备/模拟器上的当前 .apk,然后卸载run或livesync新版本。据我所知,manifest 选项是在 android 应用程序上强制一个方向的更好选择。所以它会起作用,但如果你没有卸载旧的 .apk,你可能会在设备上遇到缓存问题 :) -
我已删除 apk 并使用
tns run android运行它。我还添加了 android manifest.xml 以防我错过了什么
标签: android screen-orientation nativescript