如何查看Android手机是横屏还是竖屏?
当前回答
解决这个问题的另一种方法是不依赖于显示的正确返回值,而是依赖于Android资源的解析。
在res/values-land和res/values-port文件夹中创建文件layouts.xml,内容如下:
res / values-land / layouts.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="is_landscape">true</bool>
</resources>
res / values-port / layouts.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="is_landscape">false</bool>
</resources>
在你的源代码中,你现在可以访问当前方向,如下所示:
context.getResources().getBoolean(R.bool.is_landscape)
其他回答
还有一种方法:
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;
}
}
有很多方法可以做到这一点,这段代码适合我
if (this.getWindow().getWindowManager().getDefaultDisplay()
.getOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
// portrait mode
} else if (this.getWindow().getWindowManager().getDefaultDisplay()
.getOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
// landscape
}
老帖子我知道。不管方向是什么或者被交换了等等。我设计了这个功能,用来将设备设置在正确的方向上,而不需要知道肖像和景观特征是如何在设备上组织的。
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); }
}
效果好极了!
指定手机当前方向的完整方法:
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";
}
}
It's also worth noting that nowadays, there's less good reason to check for explicit orientation with getResources().getConfiguration().orientation if you're doing so for layout reasons, as Multi-Window Support introduced in Android 7 / API 24+ could mess with your layouts quite a bit in either orientation. Better to consider using <ConstraintLayout>, and alternative layouts dependent on available width or height, along with other tricks for determining which layout is being used, e.g. the presence or not of certain Fragments being attached to your Activity.
推荐文章
- 我如何处理ImeOptions的完成按钮点击?
- 将所有非字母数字字符替换为空字符串
- 漂亮地打印Java集合(toString不返回漂亮输出)
- 静态嵌套类在Java,为什么?
- 如何防止Eclipse在启动时挂起?
- 模块是用不兼容的Kotlin版本编译的。其元数据的二进制版本为1.5.1,预期版本为1.1.15
- Optional和Optional的区别是什么?flatMap和Optional.map?
- 如何在Android工作室添加“libs”文件夹?
- 使用什么api来绘制其他应用程序(如Facebook的Chat Heads)?
- Java中枚举的命名:单数还是复数?
- getDefaultSharedPreferences和getSharedPreferences的区别
- 如何模拟按钮点击使用代码?
- JavaBean和POJO之间的区别是什么?
- 注释在Java中如何使用,在哪里使用?
- 如何在Ubuntu下安装JDK 11 ?