【问题标题】:Find out if Android device is portrait or landscape for normal usage?了解 Android 设备是纵向还是横向以供正常使用?
【发布时间】:2011-04-10 03:36:06
【问题描述】:

是否有办法确定设备默认是纵向还是横向?我的意思是您通常如何使用该设备。

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

【问题讨论】:

标签: android default landscape portrait


【解决方案1】:

您可以这样做:

对于横向

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

纵向

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

检查:Configuration.orientation

【讨论】:

  • 当我的一项活动被锁定到清单中的特定方向时,这对我不起作用。例如,如果它被锁定为“纵向”,即使我的设备是横向平板电脑,它也会返回 Configuration.ORIENTATION_PORTRAIT。
  • 在 2017 年像冠军一样工作。
  • @jrequejo 也许使用陀螺仪数据?
  • 在 2018 年像冠军一样工作。我使用 Kotlin when 语句使语法更清晰。
【解决方案2】:

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

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

【讨论】:

    【解决方案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();
        }
    
     }
    }
    

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

    【讨论】:

      【解决方案4】:

      感谢@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();
          }
      }
      

      【讨论】:

        【解决方案5】:

        在你的活动中,这样做:

        if(getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
        

        例如...

        【讨论】:

        • 如果您在 Activity 的清单中设置了 android:screenOrientation="portrait|landscape"将不起作用。
        【解决方案6】:

        我不确定这是否会有所帮助,但这是我写的一个简单示例,它将向您展示如何拥有一个监听器...这将使您能够确定您所处的方向。

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

        【讨论】:

          【解决方案7】:

          我遇到了类似的问题。通过比较旋转和方向的值来解决它。无需使用传感器或任何侦听器,只需调用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;
                  }
              }
          }
          

          【讨论】:

            【解决方案8】:
            // 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;
            
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2023-03-30
              • 1970-01-01
              • 2013-10-17
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多