Android, landscape only orientation?

前端 未结 6 1445
半阙折子戏
半阙折子戏 2020-11-30 06:10

How can I make it so the screen orientation is always landscape?

Do I need to add something to the manifest.xml?

相关标签:
6条回答
  • 2020-11-30 06:46

    When you are in android studio 3 or above you need to add following lines AndroidManifest.xml file

    <activity
                android:name=".MainActivity"
                android:configChanges="orientation"
                android:screenOrientation= "sensorLandscape"
                tools:ignore="LockedOrientationActivity">
    

    One thing this is sensor Landscape, means it will work on both landscape sides

    But if you only want to work the regular landscape side then, replace sensorLandscape to landscape

    0 讨论(0)
  • 2020-11-30 06:50

    Add this android:screenOrientation="landscape" to your <activity> tag in the manifest for the specific activity that you want to be in landscape.

    Edit:

    To toggle the orientation from the Activity code, call setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) other parameters can be found in the Android docs for ActivityInfo.

    0 讨论(0)
  • 2020-11-30 06:54

    Yes, in AndroidManifest.xml, declare your Activity like so: <activity ... android:screenOrientation="landscape" .../>

    0 讨论(0)
  • 2020-11-30 06:56

    You can try with

     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    
    0 讨论(0)
  • 2020-11-30 07:08

    One thing I've not found through the answers is that there are two possible landscape orientations, and I wanted to let both be available! So android:screenOrientation="landscape" will lock your app only to one of the 2 possibilities, but if you want your app to be limited to both landscape orientations (for them whom is not clear, having device on portrait, one is rotating left and the other one rotating right) this is what is needed:

    android:screenOrientation="sensorLandscape"
    
    0 讨论(0)
  • 2020-11-30 07:10

    Just two steps needed:

    1. Apply setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); after setContentView().

    2. In the AndroidMainfest.xml, put this statement <activity android:name=".YOURCLASSNAME" android:screenOrientation="landscape" />

    Hope it helps and happy coding :)

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