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

前端 未结 9 1651
自闭症患者
自闭症患者 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 08:58

    Just Add:

    android:screenOrientation="portrait"
    

    in "AndroidManifest.xml" :

    <activity
    android:screenOrientation="portrait"
    android:name=".MainActivity"
    android:label="@string/app_name">
    </activity>
    
    0 讨论(0)
  • 2020-12-02 09:03

    You just have to define the property below inside the activity element in your AndroidManifest.xml file. It will restrict your orientation to portrait.

    android:screenOrientation="portrait"

    Example:

            <activity
                android:name="com.example.demo_spinner.MainActivity"
                android:label="@string/app_name"
                android:screenOrientation="portrait" >
            </activity>
    

    Additionaly, as per Eduard Luca's comment below, you can also use screenOrientation="sensorPortrait" if you want to enable rotation by 180 degrees.

    0 讨论(0)
  • 2020-12-02 09:08
    android:screenOrientation="locked"
    

    in <application> for all app in <activity> for actual activity

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

    It might be.. you have to identify it is tablet or phone by programmatically... First check device is phone or tablet

    Determine if the device is a smartphone or tablet?

    Tablet or Phone - Android

    Then......

    if(isTablet)
    {
          setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);      
    }else
    {
          setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
    
    0 讨论(0)
  • 2020-12-02 09:10

    I can see you have accepted an answer which doesn't solve your problem entirely:

    android:screenOrientation="portrait" 
    

    This will force your app to be portrait on both phones and tablets.

    You can have the app forced in the device's "preferred" orientation by using

    android:screenOrientation="nosensor"
    

    This will lead to forcing your app to portrait on most phones phones and landscape on tablets. There are many phones with keypads which were designed for landscape mode. Forcing your app to portrait can make it almost unusable on such devices. Android is recently migrating to other types of devices as well. It is best to just let the device choose the preferred orientation.

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

    You have to add the android:screenOrientation="portrait" directive in your AndroidManifest.xml. This is to be done in your <activity> tag.

    In addition, the Android Developers guide states that :

    [...] you should also explicitly declare that your application requires either portrait or landscape orientation with the element. For example, <uses-feature android:name="android.hardware.screen.portrait" />.

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