您好我想知道如何在nativescript中设置设备方向。具体来说,我希望我写的应用程序始终保持相同的方向(纵向),以便旋转设备不会导致它进入横向。
我尝试了nativescript-orientation插件和setOrientation。
var orientation = require('nativescript-orientation');
console.log(JSON.stringify(orientation));// outputs JS: {}
orientation.setOrientation("portrait");
但是我收到错误“无法读取属性setOrientation of undefined。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>
我的应用只是纵向。
对于android,你只需要在android:screenOrientation="portrait"
标签内的AndroidManifest.xml
中添加activity
。
对于iOS,open Xcode -> Select project on project navigator (left panel) -> Select target in middle panel -> Choose "General" tab -> Tick only "Portrait" in Deployment info section
对于iOS :(没有Xcode,只需操作一个文件。)
app/App_Resources/iOS/Info.plist
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
应用程序/ App_Resources /式IO / Info.plist中
...
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
(@mudlabs的道具,已经在评论中提到了这个解决方案。)
如果您使用的是NativeScript Sidekick,则可以从项目属性为Android和iOS设置方向。
最佳答案是正确的,但在某些情况下,只添加android:screenOrientation不起作用。
我通过添加android:screenOrientation="portrait" android:configChanges="keyboardHidden|orientation|screenSize"
并注意顺序,在configChanges之前首先注意screenOrientation。
实际上我已经在android studio和Nativescript中测试了它。