简体   繁体   English

启动活动时Tablet Android方向

[英]Tablet Android Orientation when launch Activity

I have a strange problem on orientation handling on Samsung Tablets. 我对Samsung Tablets的方向处理有一个奇怪的问题。 Basically, I have this app that it is supposed to run only in Landscape if running on tablet and portrait if i'm on a phone. 基本上,我有这个应用程序,它应该仅在平板电脑上运行时才能在横向模式下运行,而如果我在手机上运行则只能在纵向模式下运行。 To do that I check on wich device I'm, and then I use the onConfigurationChanged callback to set the right layout. 为此,我检查了我所在的设备,然后使用onConfigurationChanged回调设置正确的布局。 It works perfectly on the phone. 它可以在手机上完美运行。 But on tablet, if I'm holding it in portrait position, when launch another activity within the same app I see a flash oh the home screen (that's not cool), so the orientation (and also the android bar) goes in portrait position, the second activity is loaded in portrait (that's wrong) and just after a while the second activity change orientation to the right landscape position. 但是在平板电脑上,如果我将其保持在纵向位置,则在同一应用程序中启动另一个活动时,主屏幕(不很酷)会闪烁,所以方向(以及android栏)也将处于纵向位置,则第二项活动以纵向加载(这是错误的),不久之后,第二项活动将方向更改为正确的横向位置。 There is a way to simple load the second activity directly in landscape? 有没有一种方法可以直接在景观中简单地加载第二个活动? Thanks 谢谢

Francesco 弗朗切斯科

Since you are using the same activity just test for the android version and set the orientation programatically. 由于您使用的是同一活动,因此只需测试android版本并以编程方式设置方向即可。 Like this: 像这样:

  if(Build.VERSION.SDK_INT >= 11){
           //If build is tablet honeycomb or greater sdk such as 12, or 13
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);


    }
    else{
        //If device SDK is lower than 11 the view is set to PORTRAIT
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }

You could also do 你也可以

Build.VERSION_CODES.HONEYCOMB

To test for tablet. 测试平板电脑。 This would be more specific to honeycomb. 这将更特定于蜂窝。 You also do HONEYCOMB_MR1 & MR2. 您也可以执行HONEYCOMB_MR1和MR2。

If you want to load any activity directly in landscape then mention screenOrientation directly in a particular activity inside the AndroidManifest.xml file: 如果要直接在横向中加载任何活动,请在AndroidManifest.xml文件内的特定活动中直接提及screenOrientation:

android:screenOrientation="landscape"

this will load your activity directly in landscape orientation. 这将直接以横向加载您的活动。

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

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