Check orientation on Android phone
如何检查Android手机是横向还是纵向?
用于确定要检索哪些资源的当前配置可从Resources的
1 | getResources().getConfiguration().orientation; |
您可以通过查看其值来检查方向:
1 2 3 4 5 6 | int orientation = getResources().getConfiguration().orientation; if (orientation == Configuration.ORIENTATION_LANDSCAPE) { // In landscape } else { // In portrait } |
可以在Android Developer中找到更多信息。
如果在某些设备上使用getResources()。getConfiguration()。orientation,则会出错。我们最初在http://apphance.com中使用了这种方法。感谢Apphance的远程记录,我们可以在不同的设备上看到它,我们看到碎片在这里发挥作用。
我看到奇怪的情况:例如在HTC Desire HD上交替纵向和方形(?!):
1 2 3 4 5 | CONDITION[17:37:10.345] screen: rotation: 270 orientation: square CONDITION[17:37:12.774] screen: rotation: 0 orientation: portrait CONDITION[17:37:15.898] screen: rotation: 90 CONDITION[17:37:21.451] screen: rotation: 0 CONDITION[17:38:42.120] screen: rotation: 270 orientation: square |
或根本不改变方向:
1 2 3 4 5 | CONDITION[11:34:41.134] screen: rotation: 0 CONDITION[11:35:04.533] screen: rotation: 90 CONDITION[11:35:06.312] screen: rotation: 0 CONDITION[11:35:07.938] screen: rotation: 90 CONDITION[11:35:09.336] screen: rotation: 0 |
另一方面,width()和height()总是正确的(窗口管理器使用它,所以最好是)。我想说最好的想法是进行宽度/高度检查。如果你想一下,这正是你想要的 - 知道宽度是否小于高度(肖像),相反(横向)或它们是否相同(正方形)。
然后它归结为这个简单的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public int getScreenOrientation() { Display getOrient = getWindowManager().getDefaultDisplay(); int orientation = Configuration.ORIENTATION_UNDEFINED; if(getOrient.getWidth()==getOrient.getHeight()){ orientation = Configuration.ORIENTATION_SQUARE; } else{ if(getOrient.getWidth() < getOrient.getHeight()){ orientation = Configuration.ORIENTATION_PORTRAIT; }else { orientation = Configuration.ORIENTATION_LANDSCAPE; } } return orientation; } |
解决此问题的另一种方法是不依赖于显示器的正确返回值,而是依赖于Android资源解析。
使用以下内容在文件夹
RES /价值观土地/ layouts.xml:
1 2 3 4 | <?xml version="1.0" encoding="utf-8"?> <resources> <bool name="is_landscape">true</bool> </resources> |
RES /值端口/ layouts.xml:
1 2 3 4 | <?xml version="1.0" encoding="utf-8"?> <resources> <bool name="is_landscape">false</bool> </resources> |
在源代码中,您现在可以按如下方式访问当前方向:
1 | context.getResources().getBoolean(R.bool.is_landscape) |
一种指定手机当前方向的完整方式:
1 2 3 4 5 6 7 8 9 10 11 12 13 | public String getRotation(Context context){ final int rotation = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getOrientation(); switch (rotation) { case Surface.ROTATION_0: return"portrait"; case Surface.ROTATION_90: return"landscape"; case Surface.ROTATION_180: return"reverse portrait"; default: return"reverse landscape"; } } |
Chear
Binh Nguyen
以下是hackbod和Martijn推荐的代码片段演示如何获取屏幕方向:
?更改时触发方向:
1 2 3 4 5 6 | @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); int nCurrentOrientation = _getScreenOrientation(); _doSomeThingWhenChangeOrientation(nCurrentOrientation); } |
?获取hackbod推荐的最新定位:
1 2 3 | private int _getScreenOrientation(){ return getResources().getConfiguration().orientation; } |
?有获得当前屏幕方向的替代解决方案吗?按照Martijn解决方案:
1 2 3 4 | private int _getScreenOrientation(){ Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay(); return display.getOrientation(); } |
注意事项:
我试过两个工具? &安培; ?,但在RealDevice(NexusOne SDK 2.3)方向上,它返回错误的方向。
所以我建议使用解决方案?获得更多优势的屏幕方向:清晰,简单,像魅力一样工作。
仔细检查方向的返回,以确保我们的预期正确(可能有限取决于物理设备规格)
希望它有所帮助,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | int ot = getResources().getConfiguration().orientation; switch(ot) { case Configuration.ORIENTATION_LANDSCAPE: Log.d("my orient" ,"ORIENTATION_LANDSCAPE"); break; case Configuration.ORIENTATION_PORTRAIT: Log.d("my orient" ,"ORIENTATION_PORTRAIT"); break; case Configuration.ORIENTATION_SQUARE: Log.d("my orient" ,"ORIENTATION_SQUARE"); break; case Configuration.ORIENTATION_UNDEFINED: Log.d("my orient" ,"ORIENTATION_UNDEFINED"); break; default: Log.d("my orient","default val"); break; } |
使用
您只需要注意不同类型的景观,即设备通常使用的景观和其他景观。
仍然不明白如何管理。
一段时间过去了,因为大多数答案已经发布,有些时候已经弃用了方法和常量。
我已经更新了Jarek的代码,不再使用这些方法和常量了:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | protected int getScreenOrientation() { Display getOrient = getWindowManager().getDefaultDisplay(); Point size = new Point(); getOrient.getSize(size); int orientation; if (size.x < size.y) { orientation = Configuration.ORIENTATION_PORTRAIT; } else { orientation = Configuration.ORIENTATION_LANDSCAPE; } return orientation; } |
请注意,不再支持模式
我发现这在我测试过的所有设备上都是可靠的,与建议使用
在运行时检查屏幕方向。
1 2 3 4 5 6 7 8 9 10 11 12 | @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); // Checks the orientation of the screen if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { Toast.makeText(this,"landscape", Toast.LENGTH_SHORT).show(); } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){ Toast.makeText(this,"portrait", Toast.LENGTH_SHORT).show(); } } |
还有一种方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public int getOrientation() { if(getResources().getDisplayMetrics().widthPixels>getResources().getDisplayMetrics().heightPixels) { Toast t = Toast.makeText(this,"LANDSCAPE",Toast.LENGTH_SHORT); t.show(); return 1; } else { Toast t = Toast.makeText(this,"PORTRAIT",Toast.LENGTH_SHORT); t.show(); return 2; } } |
Android SDK可以告诉你这个很好:
1 | getResources().getConfiguration().orientation |
在API 28上于2019年进行了测试,无论用户是否设置了纵向方向,并且与另一个过时的答案相比,使用最少的代码,以下内容提供了正确的方向:
1 2 3 4 5 6 7 8 9 10 | /** @return The {@link Configuration#ORIENTATION_SQUARE}, {@link Configuration#ORIENTATION_PORTRAIT}, {@link Configuration#ORIENTATION_LANDSCAPE} constants based on the current phone screen pixel relations. */ private int getScreenOrientation() { DisplayMetrics dm = context.getResources().getDisplayMetrics(); // Screen rotation effected if(dm.widthPixels == dm.heightPixels) return Configuration.ORIENTATION_SQUARE; else return dm.widthPixels < dm.heightPixels ? Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE; } |
我认为这个代码可能会在方向更改生效后起作用
1 2 3 | Display getOrient = getWindowManager().getDefaultDisplay(); int orientation = getOrient.getOrientation(); |
如果要在调用setContentView之前获得有关新方向的通知,请覆盖Activity.onConfigurationChanged(Configuration newConfig)函数并使用newConfig方向。
我认为使用getRotationv()没有用,因为
http://developer.android.com/reference/android/view/Display.html#getRotation%28%29
getRotation()从"自然"方向返回屏幕的旋转。
因此,除非你知道"自然"方向,否则轮换是毫无意义的。
我找到了一种更简单的方法
1 2 3 4 5 6 7 |
请告诉我这个人是否有问题?
老帖子我知道。无论方向是什么或交换等等,我设计了这个功能,用于将设备设置在正确的方向,而无需知道如何在设备上组织纵向和横向功能。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | private void initActivityScreenOrientPortrait() { // Avoid screen rotations (use the manifests android:screenOrientation setting) // Set this to nosensor or potrait // Set window fullscreen this.activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); DisplayMetrics metrics = new DisplayMetrics(); this.activity.getWindowManager().getDefaultDisplay().getMetrics(metrics); // Test if it is VISUAL in portrait mode by simply checking it's size boolean bIsVisualPortrait = ( metrics.heightPixels >= metrics.widthPixels ); if( !bIsVisualPortrait ) { // Swap the orientation to match the VISUAL portrait mode if( this.activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT ) { this.activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } else { this.activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ); } } else { this.activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR); } } |
奇迹般有效!
用这种方式,
1 2 3 4 5 6 7 8 9 | int orientation = getResources().getConfiguration().orientation; String Orintaion =""; switch (orientation) { case Configuration.ORIENTATION_UNDEFINED: Orintaion ="Undefined"; break; case Configuration.ORIENTATION_LANDSCAPE: Orintaion ="Landscrape"; break; case Configuration.ORIENTATION_PORTRAIT: Orintaion ="Portrait"; break; default: Orintaion ="Square";break; } |
在字符串中你有Oriantion
有很多方法可以做到这一点,这段代码对我有用
1 2 3 4 5 6 7 | if (this.getWindow().getWindowManager().getDefaultDisplay() .getOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) { // portrait mode } else if (this.getWindow().getWindowManager().getDefaultDisplay() .getOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) { // landscape } |
简单易用:)
在java文件中,写:
1 | private int intOrientation; |
在
1 2 3 4 5 | intOrientation = getResources().getConfiguration().orientation; if (intOrientation == Configuration.ORIENTATION_PORTRAIT) setContentView(R.layout.activity_main); else setContentView(R.layout.layout_land); // I tested it and it works fine. |
我认为这个解决方案很简单
1 2 3 4 5 | if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){ user_todat_latout = true; } else { user_todat_latout = false; } |
这样就覆盖了所有手机,如oneplus3
1 2 3 | public static boolean isScreenOriatationPortrait(Context context) { return context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT; } |
正确的代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 | public static int getRotation(Context context){ final int rotation = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getOrientation(); if(rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180){ return Configuration.ORIENTATION_PORTRAIT; } if(rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270){ return Configuration.ORIENTATION_LANDSCAPE; } return -1; } |
值得注意的是,现在,如果你出于布局原因这样做,那么用
你可以使用它(基于这里):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | public static boolean isPortrait(Activity activity) { final int currentOrientation = getCurrentOrientation(activity); return currentOrientation == ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT || currentOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; } public static int getCurrentOrientation(Activity activity) { //code based on https://www.captechconsulting.com/blog/eric-miles/programmatically-locking-android-screen-orientation final Display display = activity.getWindowManager().getDefaultDisplay(); final int rotation = display.getRotation(); final Point size = new Point(); display.getSize(size); int result; if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) { // if rotation is 0 or 180 and width is greater than height, we have // a tablet if (size.x > size.y) { if (rotation == Surface.ROTATION_0) { result = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; } else { result = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE; } } else { // we have a phone if (rotation == Surface.ROTATION_0) { result = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; } else { result = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT; } } } else { // if rotation is 90 or 270 and width is greater than height, we // have a phone if (size.x > size.y) { if (rotation == Surface.ROTATION_90) { result = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; } else { result = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE; } } else { // we have a tablet if (rotation == Surface.ROTATION_90) { result = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT; } else { result = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; } } } return result; } |