简体   繁体   English

在整个App中将android方向锁定为横向?

[英]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 . 在Manifest中,您可以将screenOrientation设置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. 您已经放置了one activity因此其他活动以纵向方式打开,因此,为了fixing all your activities with orientationall your activities with orientation为第一个活动。 It would look something like this in the XML: 它在XML中看起来像这样:

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

You can also use the following in the onCreate() method: 您还可以在onCreate()方法中使用以下内容:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

Greetings! 问候!

Hey check this out In the androidmanifest file inside activity add it 嘿检查这个在androidmanifest文件里面的活动添加它

<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. 假如您的应用程序有三个活动,那么您必须将每个活动指定为横向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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