Force “portrait” orientation mode

2019-01-01 08:17发布

I'm trying to force the "portrait" mode for my application because my application is absolutely not designed for the "landscape" mode.

After reading some forums, I added these lines in my manifest file:

<application 
  android:debuggable="true"
  android:icon="@drawable/icon" 
  android:label="@string/app_name"
  android:screenOrientation="portrait">

But it doesn't work on my device (HTC Desire). It switches from "portrait" lo "landscape", ignoring the lines from the manifest file.

After more forums reading, I tried to add this in my manifest file:

<application 
  android:debuggable="true"
  android:icon="@drawable/icon" 
  android:label="@string/app_name"
  android:configChanges="orientation"       
  android:screenOrientation="portrait">

and this function in my activity class:

public void onConfigurationChanged(Configuration newConfig)
{
    super.onConfigurationChanged(newConfig);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

But again, no luck.

11条回答
琉璃瓶的回忆
2楼-- · 2019-01-01 08:54

If you are having a lot activity like mine, in your application Or if you dont want to enter the code for each activity tag in manifest you can do this .

in your Application Base class you will get a lifecycle callback

so basically what happens in for each activity when creating the on create in Application Class get triggered here is the code ..

public class MyApplication extends Application{

@Override
    public void onCreate() {
        super.onCreate();  

  registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle bundle) {
                activity.setRequestedOrientation(
                        ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);


// for each activity this function is called and so it is set to portrait mode


            }

            @Override
            public void onActivityStarted(Activity activity) {

            }

            @Override
            public void onActivityResumed(Activity activity) {

            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {

            }
        });
}

i hope this helps.

查看更多
看风景的人
3楼-- · 2019-01-01 08:54

Something to complement: I have updated an app recently, the previous was working in both landscape and portrait mode, and I want the updated version should work in portrait mode, so I added

android:screenOrientation="portrait"

to the corresponding activity, and it just crashed when I tested the update. Then I added

android:configChanges="orientation|keyboardHidden"

too, and it works.

查看更多
一个人的天荒地老
4楼-- · 2019-01-01 08:57

I think you want to add android:configChanges="orientation|keyboardHidden" to your activity? Otherwise the activity is restarted on config-change. The onConfigurationChanged would not be called then, only the onCreate

查看更多
时光乱了年华
5楼-- · 2019-01-01 08:58

Set force Portrait or Landscape mode, Add lines respectively.

Import below line:

import android.content.pm.ActivityInfo;

Add Below line just above setContentView(R.layout.activity_main);

For Portrait:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//Set Portrait

For Landscap:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//Set Landscape

This will definitely work.

查看更多
浮光初槿花落
6楼-- · 2019-01-01 09:03

According to Android's documentation, you should also often include screenSize as a possible configuration change.

android:configChanges="orientation|screenSize"

If your application targets API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), then you should also declare the "screenSize" configuration, because it also changes when a device switches between portrait and landscape orientations.

Also, if you all include value keyboardHidden in your examples, shouldn't you then also consider locale, mcc, fontScale, keyboard and others?..

查看更多
登录 后发表回答