How to Disable landscape mode in Android?

前端 未结 30 2663
Happy的楠姐
Happy的楠姐 2020-11-22 13:45

How can I disable landscape mode for some of the views in my Android app?

相关标签:
30条回答
  • 2020-11-22 14:16

    Add this android:screenOrientation="portrait" in your manifest file where you declare your activity like this

    <activity android:name=".yourActivity"
        ....
        android:screenOrientation="portrait" />
    

    If you want to do using java code try

    setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
    

    before you call setContentView method for your activity in onCreate().

    Hope this help and easily understandable for all...

    0 讨论(0)
  • 2020-11-22 14:16

    Add android:screenOrientation="portrait" to the activity you want to disable landscape mode.

    0 讨论(0)
  • 2020-11-22 14:19

    If you want user-settings,

    then I'd recommend setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    You can change the settings from a settings menu.

    I require this because my timers must correspond to what's on the screen, and rotating the screen will destroy the current activity.

    0 讨论(0)
  • 2020-11-22 14:19

    If you are using Xamarin C# some of these solutions will not work. Here is the solution I found to work.

    [Activity(MainLauncher = true, Icon = "@drawable/icon", ScreenOrientation = ScreenOrientation.Portrait)]
    

    Above the class works well, similar to the other solutions, also not globally applicable and needs to be placed in each activity header.

    0 讨论(0)
  • 2020-11-22 14:22

    In the <apphome>/platform/android directory created AndroidManifest.xml (copying it from the generated one). Then add android:screenOrientation="portrait" to ALL of the activity elements.

    0 讨论(0)
  • 2020-11-22 14:23

    If you want to disable Landscape mode for your android app ( or a single activity) all you need to do is add,

    android:screenOrientation="portrait" to the activity tag in AndroidManifest.xml file.

    Like:

    <activity android:name="YourActivityName" 
        android:icon="@drawable/ic_launcher" 
        android:label="Your App Name" 
        android:screenOrientation="portrait">
    

    Another Way , Programmatic Approach.

    If you want to do this programatically ie. using Java code. You can do so by adding the below code in the Java class of the activity that you don't want to be displayed in landscape mode.

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    

    I hope it helps you .For more details you can visit here enter link description here

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