锁定设备方向 - React Native(Android)

问题描述 投票:12回答:6

我正在使用React Native 0.29并为Android开发。我正试图锁定设备方向。我需要的是以纵向模式锁定屏幕。我尝试使用此存储库https://github.com/yamill/react-native-orientation但它尚未支持RN 0.29。

有什么方法可以锁定设备方向吗?也许任何原生的Android黑客与Android工作室?

android react-native orientation device-orientation
6个回答
25
投票

只需将android:screenOrientation =“portrait”添加到AndroidManifest.xml中的活动即可。


2
投票

有一个拉动请求可以在0.29.2及以上版本上工作:https://github.com/yamill/react-native-orientation/pull/85

如果您使用他的版本,它应该适用于0.29.2及以上:https://github.com/youennPennarun/react-native-orientation

脚步:

  1. 取消以前的安装与rnpm unlink react-native-orientation链接
  2. rm -rf node_modules/react-native-orientation
  3. 在你的package.json中编辑react-native-orientation的条目为: "react-native-orientation": "youennPennarun/react-native-orientation"
  4. npm install
  5. react-native link react-native-orientation

事情应该在这之后工作。您可以跟踪PR的进度并在合并后切换到主仓库。


1
投票

2017年更新

目前,通过添加以下内容,还有另一种方法可以为Android和iOS执行一次:

"orientation": "portrait"

app.json,如果你正在使用世博会:

{
  "expo": {
    "name": "My app",
    "slug": "my-app",
    "sdkVersion": "21.0.0",
    "privacy": "public",
    "orientation": "portrait"
  }
}

或者在运行时:

ScreenOrientation.allow()

例:

ScreenOrientation.allow(ScreenOrientation.Orientation.PORTRAIT);

请注意,它仅适用于您正在使用Expo进行构建,但由于这是当前(截至2017年)在React Native Blog官方指南中推荐的,因此可能很多人都在使用它,因此值得一提的是除了黑客之外的有趣解决方案特定于Android的XML配置文件。

更多信息:

有关更多信息,请参阅:how to disable rotation in React Native?


0
投票

react-native-orientation - 不再与新版本兼容(我已经尝试过0.39.2)。链接此模块后,我有编译器的错误。我得到它,现在我们应该使用react-native-orientation-listener

npm install --save react-native-orientation-listener rnpm链接


0
投票

步骤1

npm install git+https://github.com/yamill/react-native-orientation.git --save

步骤2:react-native链接步骤:3使用以下命令修改MainApplication.java文件:

import com.github.yamill.orientation.OrientationPackage;// import

@Override
protected List getPackages() {
return Arrays.asList(
new MainReactPackage(),
new OrientationPackage() //add this
);
}

0
投票

你可以使用react-native-orientation-locker

'react-native-orientation'已被弃用。使用'react-native-orientation-locker'组件,您将能够检测当前方向,并使用以下方法将其锁定到纵向/横向:

Orientation.lockToPortrait();

Orientation.lockToLandscapeLeft();

甚至使用释放锁

Orientation.unlockAllOrientations();
© www.soinside.com 2019 - 2024. All rights reserved.