2010-09-09 70 views
31

有无论如何找出一个设备默认是纵向还是横向?我的意思是你通常如何使用该设备。找出Android设备是正常使用的纵向还是横向?

大多数手机都有一个正常使用的肖像屏幕,但是有一些标志可以找出来吗?

+0

您的意思是查找应用程序运行时检查天气设备是横向还是纵向? – Herry 2011-05-21 11:22:32

+0

[如何在Android上检查设备自然(默认)方向(例如,获取横向,例如,Motorola Charm或Flipout)](http://stackoverflow.com/questions/4553650/how-to-check-device- natural-default-orientation-on-android-ie-get-landscape) – weston 2014-05-13 11:24:15

回答

2
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(); 
    } 

} 
} 

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

116

你可以这样做:

对于Lanscape

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

肖像

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

检查:http://developer.android.com/reference/android/content/res/Configuration.html#orientation

+2

当我的其中一个活动被锁定到Manifest中的特定方向时,这不适用于我。例如,如果它被锁定为“肖像”,即使我的设备是横向平板电脑,它也会返回Configuration.ORIENTATION_PORTRAIT。 – jrequejo 2015-03-13 13:17:23

+1

像2017年的冠军一样工作。 – 2017-07-07 14:01:50

+0

@jrequejo也许使用陀螺仪数据? – zundi 2017-10-19 19:34:31

0

我不知道,如果这将有助于但是这是一个简单的例子,我写,会告诉你如何有一个监听器...,这将让你弄清楚你是在什么方向

...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); 

      } 


     } 

    };... 


[email protected] 
protected void onDestroy() { 
    // TODO Auto-generated method stub 
    super.onDestroy(); 
    myOrientationEventListener.disable(); 
}... 
2

在你actvity,做到这一点:

if(getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) 

例如...

1

感谢@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(); 
    } 
} 
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; 
     } 
    } 
} 
相关问题