简体   繁体   中英

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.

I know the solution for iPad/iPhone, so it would be good if anyone can help me with the android configuration

Please add the below code to android/app/src/main/java/com/[your project name]/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

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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