如何查看Android手机是横屏还是竖屏?


当前回答

老帖子我知道。不管方向是什么或者被交换了等等。我设计了这个功能,用来将设备设置在正确的方向上,而不需要知道肖像和景观特征是如何在设备上组织的。

   private void initActivityScreenOrientPortrait()
    {
        // Avoid screen rotations (use the manifests android:screenOrientation setting)
        // Set this to nosensor or potrait

        // Set window fullscreen
        this.activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        DisplayMetrics metrics = new DisplayMetrics();
        this.activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

         // Test if it is VISUAL in portrait mode by simply checking it's size
        boolean bIsVisualPortrait = ( metrics.heightPixels >= metrics.widthPixels ); 

        if( !bIsVisualPortrait )
        { 
            // Swap the orientation to match the VISUAL portrait mode
            if( this.activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT )
             { this.activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); }
            else { this.activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ); }
        }
        else { this.activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR); }

    }

效果好极了!

其他回答

Android SDK可以很好地告诉你:

getResources().getConfiguration().orientation

简单易行:)

制作2个xml布局(即纵向和横向) 在java文件中,写入: private int intOrientation; 在onCreate方法和setContentView之前写入: intOrientation = getResources().getConfiguration().orientation; if (intOrientation == Configuration.ORIENTATION_PORTRAIT) setContentView (R.layout.activity_main); 其他的 setContentView (R.layout.layout_land);//我测试了它,它工作正常。

It's also worth noting that nowadays, there's less good reason to check for explicit orientation with getResources().getConfiguration().orientation if you're doing so for layout reasons, as Multi-Window Support introduced in Android 7 / API 24+ could mess with your layouts quite a bit in either orientation. Better to consider using <ConstraintLayout>, and alternative layouts dependent on available width or height, along with other tricks for determining which layout is being used, e.g. the presence or not of certain Fragments being attached to your Activity.

我认为这段代码可以在方向改变生效后工作

Display getOrient = getWindowManager().getDefaultDisplay();

int orientation = getOrient.getOrientation();

覆盖的活动。onConfigurationChanged(Configuration newConfig)函数,如果你想在调用setContentView之前得到关于新方向的通知,请使用newConfig,orientation。

大多数答案已经发布了一段时间,其中一些使用了现在已弃用的方法和常量。

我已经更新了Jarek的代码,不再使用这些方法和常量:

protected int getScreenOrientation()
{
    Display getOrient = getWindowManager().getDefaultDisplay();
    Point size = new Point();

    getOrient.getSize(size);

    int orientation;
    if (size.x < size.y)
    {
        orientation = Configuration.ORIENTATION_PORTRAIT;
    }
    else
    {
        orientation = Configuration.ORIENTATION_LANDSCAPE;
    }
    return orientation;
}

注意配置模式。不再支持ORIENTATION_SQUARE。

与建议使用getResources().getConfiguration().orientation的方法相比,我发现这个方法在我测试过的所有设备上都是可靠的