2010-09-09 61 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; 
     } 
    } 
} 
相關問題