如何查看Android手机是横屏还是竖屏?
当前回答
下面是hackbod和Martijn推荐的如何获得屏幕方向的代码片段演示:
改变时触发
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
int nCurrentOrientation = _getScreenOrientation();
_doSomeThingWhenChangeOrientation(nCurrentOrientation);
}
将当前方向设置为hackbod
private int _getScreenOrientation(){
return getResources().getConfiguration().orientation;
}
还有其他方法可以获得当前屏幕方向,参照Martijn方案:
private int _getScreenOrientation(){
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
return display.getOrientation();
}
★注意: 我试过这两种实现,但在RealDevice (NexusOne SDK 2.3) Orientation上,它返回了错误的方向。
★所以我推荐使用解决方案来获得屏幕定位,有更多的优势:清楚,简单,工作就像一个魅力。
★仔细检查方向回归,以确保符合我们的预期(可能有限制取决于物理设备规格)
希望能有所帮助,
其他回答
用这种方法,
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;
}
在字符串中,你有方向
下面是hackbod和Martijn推荐的如何获得屏幕方向的代码片段演示:
改变时触发
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
int nCurrentOrientation = _getScreenOrientation();
_doSomeThingWhenChangeOrientation(nCurrentOrientation);
}
将当前方向设置为hackbod
private int _getScreenOrientation(){
return getResources().getConfiguration().orientation;
}
还有其他方法可以获得当前屏幕方向,参照Martijn方案:
private int _getScreenOrientation(){
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
return display.getOrientation();
}
★注意: 我试过这两种实现,但在RealDevice (NexusOne SDK 2.3) Orientation上,它返回了错误的方向。
★所以我推荐使用解决方案来获得屏幕定位,有更多的优势:清楚,简单,工作就像一个魅力。
★仔细检查方向回归,以确保符合我们的预期(可能有限制取决于物理设备规格)
希望能有所帮助,
简单易行:)
制作2个xml布局(即纵向和横向) 在java文件中,写入: private int intOrientation; 在onCreate方法和setContentView之前写入: intOrientation = getResources().getConfiguration().orientation; if (intOrientation == Configuration.ORIENTATION_PORTRAIT) setContentView (R.layout.activity_main); 其他的 setContentView (R.layout.layout_land);//我测试了它,它工作正常。
当前配置,用于确定检索哪些资源,可以从resources的configuration对象中获得:
getResources().getConfiguration().orientation;
你可以通过查看它的值来检查方向:
int orientation = getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
// In landscape
} else {
// In portrait
}
更多信息可以在Android开发者中找到。
有很多方法可以做到这一点,这段代码适合我
if (this.getWindow().getWindowManager().getDefaultDisplay()
.getOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
// portrait mode
} else if (this.getWindow().getWindowManager().getDefaultDisplay()
.getOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
// landscape
}
推荐文章
- 将焦点设置为EditText
- 如何把空格字符放入XML字符串名?
- 在Java中从HashMap获取键
- 如何在Java中安排一个周期性的任务?
- 如何使一个Java通用方法静态?
- 我可以设置“android:layout_below”在运行时编程?
- for-each循环和迭代器,哪个更有效?
- 泛型类中的静态方法?
- 如何在JPA中持久化类型列表<字符串>的属性?
- 考虑在配置中定义一个'package'类型的bean [Spring-Boot]
- Java注释中的/**和/*
- java8 LocalDate Jackson格式
- Android Studio谷歌JAR文件导致GC开销限制超过错误
- 如何在Intellij生成串行版本UID
- 为什么每当我创建一个新项目时,Eclipse都会自动添加appcompat v7库支持?