我正在使用React Native 0.29并进行Android开发。我正在尝试锁定设备方向,我需要的是将屏幕锁定为竖屏模式。我尝试使用这个仓库https://github.com/yamill/react-native-orientation,但它还不支持RN 0.29。
是否有任何方法可以锁定设备方向?也许有任何使用Android Studio的本地Android hack吗?
是否有任何方法可以锁定设备方向?也许有任何使用Android Studio的本地Android hack吗?
只需在AndroidManifest.xml中的活动中添加android:screenOrientation="portrait"。
目前,如果您使用Expo,则可以通过在app.json
中添加:
"orientation": "portrait"
一次性为Android和iOS完成它的另一种方法。
{
"expo": {
"name": "My app",
"slug": "my-app",
"sdkVersion": "21.0.0",
"privacy": "public",
"orientation": "portrait"
}
}
ScreenOrientation.allow()
例子:
ScreenOrientation.allow(ScreenOrientation.Orientation.PORTRAIT);
rnpm unlink react-native-orientation
rm -rf node_modules/react-native-orientation
react-native-orientation
的条目编辑为:
"react-native-orientation": "youennPennarun/react-native-orientation"
npm install
react-native link react-native-orientation
react-native-orientation - 不再与新版本兼容(我已尝试了0.39.2)。在链接此模块后,我遇到了编译器错误。 据我所知,现在我们应该使用react-native-orientation-listener
npm install --save react-native-orientation-listener
rnpm link
您可以使用react-native-orientation-locker。
'react-native-orientation'已被弃用。 使用'react-native-orientation-locker'组件,您将能够检测当前方向,并通过以下方式锁定为纵向/横向:
Orientation.lockToPortrait();
Orientation.lockToLandscapeLeft();
甚至可以使用释放锁
Orientation.unlockAllOrientations();
步骤:1
npm install git+https://github.com/yamill/react-native-orientation.git --save
步骤2: react-native link 步骤3: 使用以下内容修改MainApplication.java文件:
import com.github.yamill.orientation.OrientationPackage;// import
@Override
protected List getPackages() {
return Arrays.asList(
new MainReactPackage(),
new OrientationPackage() //add this
);
}