Android - Vertical layout only

后端 未结 8 1639
無奈伤痛
無奈伤痛 2021-01-31 07:26

How do I make sure my app is only for vertical layout?

I have tried android:screenOrientation=\"portrait\" but that doesn\'t seem to do the trick.

相关标签:
8条回答
  • 2021-01-31 07:32

    Just to confirm Pentium10's answer.
    I was having a similar issue and adding android:screenOrientation="portrait" to the activity tag.
    Did the trick for me.

    0 讨论(0)
  • 2021-01-31 07:36

    In your manifest under activity add this :

    <activity
            android:name="com.zeus.MyProject"
            android:screenOrientation="portrait"
            >
    
    0 讨论(0)
  • 2021-01-31 07:36

    Activity is to block just in case want to block all and only repeat this line of code in their Activitys.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
    
        // Here, talk to the java does not want that the user can rotate their activity.
    
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    

    or open your "AndroidManisfest.xml" and add the rows for the portrait mode as shown below.

            android:configChanges="orientation"
            android:screenOrientation="portrait">
    
    0 讨论(0)
  • 2021-01-31 07:38

    You need to add to all your activity not for one only. I think you understood that setting is per application wide, but it isn't.

    <activity android:name=".MyActivity"
              android:label="My Activity"
              android:screenOrientation="portrait">
    

    Add the declaration to the activity tag in AndroidManifest for every Activity you want to be portrait-only.

    0 讨论(0)
  • 2021-01-31 07:43

    you have to change into AndroidManifest.xml.

    for each activity you have to insert:

    android:configChanges = "orientation"
    
    android:screenOrientation = "portrait"
    

    for e.g.:

    <activity android:name=".YourActivityName"
                      android:label="@string/app_name"
                      android:configChanges = "orientation"
                      android:screenOrientation = "portrait">
    

    This works for a single activity.. But there seems no application wide setting there.

    0 讨论(0)
  • 2021-01-31 07:47

    If you want some group of your activities to be locked on PORTRAIT mode only, than you can choose the next way:

    public abstract class BasePortraitActivity extends Activity {
    
        @Override
        protected final void onCreate(Bundle state) {
            super.onCreate(state);
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            performOnCreate(state);
        }
    
        protected abstract void performOnCreate(Bundle state);
    
    }
    

    And than just extend BasePortraitActivity where you need it. Or just add setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); to YourActivity.onCreate().

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