55

无论如何要找出设备默认是纵向还是横向的?我的意思是您通常如何使用该设备。

大多数手机都有一个正常使用的纵向屏幕,但是否有一些标志可以发现它?

4

8 回答 8

175

您可以通过以下方式做到这一点:

对于景观

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

肖像

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

检查:Configuration.orientation

于 2011-05-21T11:58:23.517 回答
10

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

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
    // landscape
} else {
    // portrait
}
于 2019-02-22T17:01:18.473 回答
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();
    }

 }
}

您可以尝试使用此代码检查设备是横向还是纵向。

于 2011-05-21T11:43:31.973 回答
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();
    }
}
于 2016-02-24T20:50:50.953 回答
2

在您的活动中,请执行以下操作:

if(getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)

例如...

于 2014-09-02T05:42:22.367 回答
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();
}...
于 2011-06-23T02:07:37.803 回答
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;
        }
    }
}
于 2017-04-28T08:10:10.650 回答
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;

}
于 2018-05-20T00:14:27.570 回答