简体   繁体   English

React-Native Android方向

[英]React-Native Android orientation

I am building a react-native app where I want to enable (portrait + landscape) mode for android tablet and portrait mode for android mobile.我正在构建一个 react-native 应用程序,我想为 android 平板电脑启用(纵向+横向)模式,为 android 手机启用纵向模式。

I know the solution for iPad/iPhone, so it would be good if anyone can help me with the android configuration我知道 iPad/iPhone 的解决方案,所以如果有人能帮我解决 android 配置,那就太好了

Please add the below code to android/app/src/main/java/com/[your project name]/MainActivity.java请将以下代码添加到 android/app/src/main/java/com/[您的项目名称]/MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getResources().getBoolean(R.bool.portrait_only)) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }else{
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
    }
}

After that create three bools.xml files之后创建三个 bools.xml 文件

android/app/src/main/res/values/bools.xml android/app/src/main/res/values/bools.xml

<?xml version="1.0" encoding="utf-8"?>
    <resources>
        <bool name="portrait_only">true</bool>
    </resources>

android/app/src/main/res/values-sw600dp/bools.xml android/app/src/main/res/values-sw600dp/bools.xml

<?xml version="1.0" encoding="utf-8"?>
    <resources>
        <bool name="portrait_only">false</bool>
    </resources>

android/app/src/main/res/values-xlarge/bools.xml android/app/src/main/res/values-xlarge/bools.xml

<?xml version="1.0" encoding="utf-8"?>
    <resources>
        <bool name="portrait_only">false</bool>
    </resources>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM