Java 如何获得当前的屏幕方向?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3663665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get the current screen orientation?
提问by Sheehan Alam
I just want to set some flags when my orientation is in landscape so that when the activity is recreated in onCreate() i can toggle between what to load in portrait vs. landscape. I already have a layout-land xml that is handling my layout.
我只是想在我的方向处于横向时设置一些标志,以便在 onCreate() 中重新创建活动时,我可以在纵向加载和横向加载之间切换。我已经有一个 layout-land xml 来处理我的布局。
public void onConfigurationChanged(Configuration _newConfig) {
if (_newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
this.loadURLData = false;
}
if (_newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
this.loadURLData = true;
}
super.onConfigurationChanged(_newConfig);
}
Over-riding onConfigurationChanged will prevent my layout-land xml from loading in landscape orientation.
覆盖 onConfigurationChanged 将阻止我的 layout-land xml 以横向加载。
I just want to get the current orientation of my device in onCreate(). How can I get this?
我只想在 onCreate() 中获取设备的当前方向。我怎样才能得到这个?
采纳答案by EboMike
Activity.getResources().getConfiguration().orientation
回答by Sakthimuthiah
In some devices void onConfigurationChanged()
may crash. User will use this code to get current screen orientation.
在某些设备中void onConfigurationChanged()
可能会崩溃。用户将使用此代码获取当前屏幕方向。
public int getScreenOrientation()
{
Display getOrient = getActivity().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;
}
And use
并使用
if (orientation==1) // 1 for Configuration.ORIENTATION_PORTRAIT
{ // 2 for Configuration.ORIENTATION_LANDSCAPE
//your code // 0 for Configuration.ORIENTATION_SQUARE
}
回答by Rudolf Coutinho
getActivity().getResources().getConfiguration().orientation
this command returns int value 1 for Portrait and 2 for Landscape
此命令为纵向返回 int 值 1,为横向返回 2
回答by a.ch.
In case anyone would like to obtain meaningfulorientation description (like that passed to onConfigurationChanged(..)
with those reverseLandscape
, sensorLandscape
and so on), simply use getRequestedOrientation()
如果有人想获得有意义的方向描述(比如传递给onConfigurationChanged(..)
那些reverseLandscape
,sensorLandscape
等等),只需使用getRequestedOrientation()
回答by AndroidBeginner
int rotation = getWindowManager().getDefaultDisplay().getRotation();
this will gives all orientation like normal and reverse
这将提供所有方向,如正常和反向
and handle it like
并像处理它一样
int angle = 0;
switch (rotation) {
case Surface.ROTATION_90:
angle = -90;
break;
case Surface.ROTATION_180:
angle = 180;
break;
case Surface.ROTATION_270:
angle = 90;
break;
default:
angle = 0;
break;
}
回答by Daniel
int orientation = this.getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
// code for portrait mode
} else {
// code for landscape mode
}
When the superclass of this
is Context
当 的超类this
是Context
回答by Samarth S
if you want to override that onConfigurationChanged method and still want it to do the basic stuff then consider using super.onConfigyrationChanged() as the first statement in the overriden method.
如果您想覆盖 onConfigurationChanged 方法并仍然希望它执行基本操作,请考虑使用 super.onConfigyrationChanged() 作为覆盖方法中的第一条语句。
回答by DalveerSinghDaiya
In your activity class use the method below :
在您的活动类中使用以下方法:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
setlogo();// Your Method
Log.d("Daiya", "ORIENTATION_LANDSCAPE");
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
setlogoForLandScape();// Your Method
Log.d("Daiya", "ORIENTATION_PORTRAIT");
}
}
Then to declare that your activity handles a configuration change, edit the appropriate element in your manifest file to include the android:configChanges
attribute with a value that represents the configuration you want to handle. Possible values are listed in the documentation for the android:configChanges
attribute (the most commonly used values are "orientation" to prevent restarts when the screen orientation changes and "keyboardHidden" to prevent restarts when the keyboard availability changes). You can declare multiple configuration values in the attribute by separating them with a pipe | character.
然后要声明您的 Activity 处理配置更改,请编辑清单文件中的相应元素,以包含android:configChanges
具有代表您要处理的配置的值的属性。android:configChanges
属性的文档中列出了可能的值(最常用的值是“orientation”以防止屏幕方向更改时重新启动和“keyboardHidden”以防止在键盘可用性更改时重新启动)。您可以在属性中声明多个配置值,方法是用竖线分隔它们 | 特点。
<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name">
That's all!!
就这样!!
回答by Z3nk
I just want to propose another alternative that will concern some of you :
我只想提出另一种可能会引起你们中某些人关注的替代方案:
android:configChanges="orientation|keyboardHidden"
implies that we explicitly declare the layout to be injected.
意味着我们明确声明要注入的布局。
In case we want to keep the automatic injection thanks to the layout-land and layout folders. All you have to do is add it to the onCreate:
如果我们想通过 layout-land 和 layout 文件夹保持自动注入。您所要做的就是将其添加到 onCreate 中:
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
getSupportActionBar().hide();
} else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
getSupportActionBar().show();
}
Here, we display or not the actionbar depending on the orientation of the phone
在这里,我们根据手机的方向显示或不显示操作栏