简体   繁体   English

Android:始终以纵向启动“活动”,但可以识别方向变化

[英]Android: start Activity always in portrait but recognize orientation change

I want to start a new activity/intent always in portrait orientation (even if the device is held in landscape) 我想始终以纵向方向开始新的活动/意图(即使设备横向放置)

If I use setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 如果我使用setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); the Activity is locked to portrait, but I want to recognize orientational changes. 活动被锁定为人像,但我想识别方向的变化。

So I think I have to call setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 所以我想我必须打电话给setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); on the first start, when the activity is created, an need an listener to remove this restriction. 在第一次创建活动时,需要一个侦听器来消除此限制。

But in which methods should I do that? 但是我应该用哪种方法呢? In the constructor , or in onCreate() or elsewhere??? 在构造函数中,还是在onCreate()或其他地方???

@Override
    public void onConfigurationChanged(Configuration newConfig) {
        // TODO Auto-generated method stub
        super.onConfigurationChanged(newConfig);

        System.out.println("on config change method called.");

        if(Configuration.ORIENTATION_LANDSCAPE==newConfig.orientation)
        {

                    //Portatrate to landscape...
            System.out.println("on config change method called portraite mode.");
        }
        else if(Configuration.ORIENTATION_PORTRAIT==newConfig.orientation)
        {
            //Landscape to portraite....
            System.out.println("on config change method called landscape mode.");
        }
        }

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

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