How to Lock Android App's Orientation to Portrait in Phones and Landscape in Tablets?

前端 未结 9 1652
自闭症患者
自闭症患者 2020-12-02 08:44

I am developing an Android app whose orientation I don\'t want changed to landscape mode when the user rotates the device. Also, I want the locked orientation to be portrait

相关标签:
9条回答
  • 2020-12-02 09:13

    Set the Screen orientation to portrait in Manifest file under the activity Tag.

    Here the example

    You need to enter in every Activity

    Add The Following Lines in Activity

    for portrait

    android:screenOrientation="portrait"
    tools:ignore="LockedOrientationActivity"
    
    

    for landscape

    android:screenOrientation="landscape"
    tools:ignore="LockedOrientationActivity"
    

    Here The Example of MainActivity

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        package="org.thcb.app">
     
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
    
            <activity android:name=".MainActivity"
                android:screenOrientation="portrait"
                tools:ignore="LockedOrientationActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
    <activity android:name=".MainActivity2"
                android:screenOrientation="landscape"
                tools:ignore="LockedOrientationActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
    
    
    0 讨论(0)
  • 2020-12-02 09:18
    <activity android:name=".yourActivity"
              android:screenOrientation="portrait" ... />
    

    add to main activity and add

    android:configChanges="keyboardHidden"
    

    to keep your program from changing mode when keyboard is called.

    0 讨论(0)
  • 2020-12-02 09:23

    Set the Screen orientation to portrait in Manifest file under the activity Tag.

    0 讨论(0)
提交回复
热议问题