如何在 nativescript 中设置方向
How to set orientation in nativescript
您好,我想知道如何在 nativescript 中设置设备方向。
具体来说,我希望我正在编写的应用程序始终保持相同的方向(纵向),以便旋转设备不会导致它进入横向。
我尝试了 nativescript-orientation 插件和 setOrientation。
var orientation = require('nativescript-orientation');
console.log(JSON.stringify(orientation));// outputs JS: {}
orientation.setOrientation("portrait");
但是我收到错误消息“无法读取 属性 setOrientation of undefined。
tns plugin list 显示插件已安装。我还尝试删除 platforms/android
目录和 运行 tns platform add android
,结果相同。
我也尝试过将 android:screenOrientation="portrait"
的各种组合放入 AndroidManifest.xml 但没有成功。
AndroidManifest.xml 从内部 App_resources 看起来像这样
<?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.xml
和 Info.plist
。
AndroidManifest.xml
将屏幕方向设置为纵向 activity
<activity android:name="com.tns.NativeScriptActivity"
...
android:screenOrientation="portrait">
Info.plist
仅保留纵向,从 UISupportedInterfaceOrientations 中删除其余部分。
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
</array>
注意:确保您在这些更改后运行一个干净的构建。
如果您使用的是 NativeScript Sidekick,则可以从项目属性中为 Android 和 iOS 设置方向。
对于iOS:(没有Xcode,只操作一个文件。)
- 打开文件
app/App_Resources/iOS/Info.plist
注释掉或删除:
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
app/App_Resources/iOS/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 的支持,他已经在评论中提到了这个解决方案。)
最上面的答案是正确的,但在某些情况下,仅添加 android:screenOrientation 是行不通的。
我通过添加 android:screenOrientation="portrait" android:configChanges="keyboardHidden|orientation|screenSize"
和 来实现它的工作,注意顺序,在 configChanges 之前首先是 screenOrientation。
实际上,我已经在 android studio 和 Nativescript 中对其进行了测试。
您好,我想知道如何在 nativescript 中设置设备方向。 具体来说,我希望我正在编写的应用程序始终保持相同的方向(纵向),以便旋转设备不会导致它进入横向。
我尝试了 nativescript-orientation 插件和 setOrientation。
var orientation = require('nativescript-orientation');
console.log(JSON.stringify(orientation));// outputs JS: {}
orientation.setOrientation("portrait");
但是我收到错误消息“无法读取 属性 setOrientation of undefined。
tns plugin list 显示插件已安装。我还尝试删除 platforms/android
目录和 运行 tns platform add android
,结果相同。
我也尝试过将 android:screenOrientation="portrait"
的各种组合放入 AndroidManifest.xml 但没有成功。
AndroidManifest.xml 从内部 App_resources 看起来像这样
<?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.xml
和 Info.plist
。
AndroidManifest.xml
将屏幕方向设置为纵向 activity
<activity android:name="com.tns.NativeScriptActivity"
...
android:screenOrientation="portrait">
Info.plist
仅保留纵向,从 UISupportedInterfaceOrientations 中删除其余部分。
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
</array>
注意:确保您在这些更改后运行一个干净的构建。
如果您使用的是 NativeScript Sidekick,则可以从项目属性中为 Android 和 iOS 设置方向。
对于iOS:(没有Xcode,只操作一个文件。)
- 打开文件
app/App_Resources/iOS/Info.plist
注释掉或删除:
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
app/App_Resources/iOS/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 的支持,他已经在评论中提到了这个解决方案。)
最上面的答案是正确的,但在某些情况下,仅添加 android:screenOrientation 是行不通的。
我通过添加 android:screenOrientation="portrait" android:configChanges="keyboardHidden|orientation|screenSize"
和 来实现它的工作,注意顺序,在 configChanges 之前首先是 screenOrientation。
实际上,我已经在 android studio 和 Nativescript 中对其进行了测试。