如何查看Android手机是横屏还是竖屏?
当前回答
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;
}
其他回答
我认为这个解决方法很简单
if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
user_todat_latout = true;
} else {
user_todat_latout = false;
}
当前配置,用于确定检索哪些资源,可以从resources的configuration对象中获得:
getResources().getConfiguration().orientation;
你可以通过查看它的值来检查方向:
int orientation = getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
// In landscape
} else {
// In portrait
}
更多信息可以在Android开发者中找到。
我认为这段代码可以在方向改变生效后工作
Display getOrient = getWindowManager().getDefaultDisplay();
int orientation = getOrient.getOrientation();
覆盖的活动。onConfigurationChanged(Configuration newConfig)函数,如果你想在调用setContentView之前得到关于新方向的通知,请使用newConfig,orientation。
我认为使用getRotationv()没有帮助,因为 http://developer.android.com/reference/android/view/Display.html#getRotation%28%29 getRotation()返回屏幕从其“自然”方向的旋转。
所以除非你知道“自然”方向,旋转是没有意义的。
我找到了一个更简单的方法
Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
if(width>height)
// its landscape
请告诉我这个人有什么问题吗?
使用getresource () .getConfiguration()。方向是正确的。
你只需要注意不同类型的景观,设备通常使用的景观和其他的。
还是不知道该怎么处理。
推荐文章
- 以编程方式将EditText的输入类型从PASSWORD更改为NORMAL,反之亦然
- 如何在隐藏和查看密码之间切换
- 在Android上调整一个大的位图文件到缩放输出文件
- 使用Enum实现单例(Java)
- 如何更改Android版本和代码版本号?
- Android Studio突然无法解析符号
- 应用程序重新启动而不是恢复
- RabbitMQ与通道和连接之间的关系
- buildSessionFactory()配置方法在Hibernate中已弃用?
- Spring MVC -如何获得所有的请求参数在一个地图在Spring控制器?
- 如何设置整个应用程序在纵向模式?
- 如何在Java中按两个字段排序?
- Android中文本的阴影效果?
- 以编程方式设置TextView的布局权重
- Android -如何覆盖“后退”按钮,所以它不完成()我的活动?