简体   繁体   中英

Lock the android orientation as landscape throughout the App?

I have checked many question based on this but still I am not able to get it how to lock the screen orientation to landscape through out the app. ?

<activity android:screenOrientation="landscape"
      android:name=".BasicLayoutCheckActivity"
        />

this is not working for me it comes back to potrait if another activity is used

In the Manifest, you can set the screenOrientation to landscape for all the activities . You have placed for one activity so other activities are opening in portrait, So for fixing set all your activities with orientation as your first activity. It would look something like this in the XML:

<activity android:name=".BasicLayoutCheckActivity" android:screenOrientation="landscape"></activity>

You can also use the following in the onCreate() method:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

Greetings!

Hey check this out In the androidmanifest file inside activity add it

<activity
android:screenOrientation="landscape"
android:configChanges="keyboard|keyboardHidden|orientation">

orientation属性必须设置为应用程序的每个单独活动。

To avoid having to do this for every activity you can register an activity lifecycle callback in your custom application class (if you have one).

Something like...

public class MyApplication extends Application {

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

        //Lock orientation in landscape for all activities, yaay!
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                 activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);                  
            }

            @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 outState) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {

            }
        });
    }
}

What do you mean by another activity? The configuration is per activity. Say if your application has three activity then you must specify each one as landscape.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM