查找Android设备是横屏还是竖屏以进行正常使用?

55

有没有办法通过默认方式查找设备是横屏还是竖屏?我的意思是您通常使用设备的方式。

大多数手机在正常使用时都具有纵向屏幕,但是否有一些标记可以找出这一点?


你的意思是在应用运行时查找设备是横向还是竖向吗? - Herry
可能是重复的问题:如何在Android上检查设备的自然(默认)方向(例如,获取横向的Motorola Charm或Flipout)?(链接:https://dev59.com/gm455IYBdhLWcg3wFP9u) - weston
9个回答

179

你可以通过以下方式实现:

针对横屏模式

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
    // Do some stuff
}

针对纵向排版

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
    // Do some stuff
}

检查:Configuration.orientation


2
当我的某个活动在清单中被锁定为特定方向时,这对我不起作用。例如,如果它被锁定为“纵向”,即使我的设备是横向平板电脑,它也会返回 Configuration.ORIENTATION_PORTRAIT。 - jrequejo
3
2017年表现出色。 - Animesh Patra
@jrequejo 或许可以使用陀螺仪数据? - zundi
在2018年表现如冠军。我使用了Kotlin的 when 语句使语法更清晰。 - AdamHurwitz

10

这很简单,只需要一个If-Else块:

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
    // landscape
} else {
    // portrait
}

3
感谢@Codeversed的出色答案,它非常接近工作状态(但如图所示并不是),我有一个MainActivity可以很好地工作。需要做的只是将.enable移动到可以在on块之外执行的位置,例如在声明myOrientationEventListener后立即执行。
import android.app.Activity;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.OrientationEventListener;

public class MainActivity extends Activity
{
    public static boolean PORTRAIT_MODE = true;
    OrientationEventListener myOrientationEventListener ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        myOrientationEventListener = new OrientationEventListener(this,
                SensorManager.SENSOR_DELAY_NORMAL)
        {
            @Override
            public void onOrientationChanged(int orientation)
            {
                PORTRAIT_MODE = ((orientation < 100) || (orientation > 280));
                Log.w("Orient", orientation + " PORTRAIT_MODE = " + PORTRAIT_MODE);
            }
        };
        Log.w("Listener", " can detect orientation: " + myOrientationEventListener.canDetectOrientation() + " ");
        myOrientationEventListener.enable();
    }
}

3
package com.android.portraitandlandscape;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.widget.Toast;

public class PortraitLandScape extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Display display = getWindowManager().getDefaultDisplay(); 
    int width = display.getWidth();
    int height = display.getHeight();

    Log.v("log_tag", "display width is "+ width);
    Log.v("log_tag", "display height is "+ height);

    if(width<height){
        Toast.makeText(getApplicationContext(),"Device is in portrait mode",Toast.LENGTH_LONG ).show();
    }
    else{
        Toast.makeText(getApplicationContext(),"Device is in landscape  mode",Toast.LENGTH_LONG ).show();
    }

 }
}

您可以尝试使用此代码来检查设备是处于横屏还是竖屏状态。

2
在您的活动中,执行以下操作:
if(getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)

例如...

如果在Activity的清单文件中设置了android:screenOrientation="portrait|landscape",则此方法将无法正常工作。 - Pierre

1

我不确定这是否有帮助,但我写了一个快速的例子,将向您展示如何拥有监听器......它将允许您确定您所处的方向。

...textviewOrientation = (TextView)findViewById(R.id.textView1);

    myOrientationEventListener = new OrientationEventListener(this, 
              SensorManager.SENSOR_DELAY_NORMAL){

        @Override
        public void onOrientationChanged(int arg0) {

         textviewOrientation.setText("Orientation: " + String.valueOf(arg0));
         myOrientationEventListener.enable();


             if ((arg0 < 100) || (arg0 > 280)){

                 setRequestedOrientation(
                     ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

             } else {

                 setRequestedOrientation(
                     ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

             }


        }

    };...


...@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    myOrientationEventListener.disable();
}...

1
我遇到过类似的问题。通过比较旋转和方向的值来解决它。不需要使用传感器或任何监听器,只需在需要时调用isDefaultLandscape方法即可。
private boolean isDefaultLandscape(final Context context)
{
    Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    int rotation = display.getRotation();
    int orientation = context.getResources().getConfiguration().orientation;

    switch (rotation)
    {
        case Surface.ROTATION_180:
        case Surface.ROTATION_0:
        {
            return orientation == Configuration.ORIENTATION_LANDSCAPE;
        }
        default:
        {
            return orientation == Configuration.ORIENTATION_PORTRAIT;
        }
    }
}

0
// Current orientation
public boolean landscape = false;

public boolean isLandscape(){

    DisplayMetrics displaymetrics = new DisplayMetrics();
    context.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int width = displaymetrics.widthPixels;
    int height = displaymetrics.heightPixels;

    if(width<height){
        landscape = false;
    }
    else{
        landscape = true;
    }

    return landscape;

}

0
private boolean isPortraitMode() {
    int orientation = context.getResources().getConfiguration().orientation;
    if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
      return false;
    }
    if (orientation == Configuration.ORIENTATION_PORTRAIT) {
      return true;
    }
    Log.d(TAG, "isPortraitMode returning false by default");
    return false;
  }

1
由于目前您的回答不够清晰,请编辑以添加更多细节,以帮助其他人理解它如何回答所提出的问题。您可以在帮助中心中找到有关编写良好答案的更多信息。 - Community

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接