hac*_*bod 645
用于确定要检索哪些资源的当前配置可从Resources的Configuration对象获得:
getResources().getConfiguration().orientation;
Run Code Online (Sandbox Code Playgroud)
您可以通过查看其值来检查方向:
int orientation = getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
// In landscape
} else {
// In portrait
}
Run Code Online (Sandbox Code Playgroud)
可以在Android Developer中找到更多信息.
Jar*_*iuk 170
如果在某些设备上使用getResources().getConfiguration().orientation,则会出错.我们最初在http://apphance.com中使用了这种方法.感谢Apphance的远程记录,我们可以在不同的设备上看到它,我们看到碎片在这里发挥作用.我看到奇怪的情况:例如在HTC Desire HD上交替纵向和方形(?!):
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
Run Code Online (Sandbox Code Playgroud)
或根本不改变方向:
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
Run Code Online (Sandbox Code Playgroud)
另一方面,width()和height()总是正确的(窗口管理器使用它,所以最好是).我想说最好的想法是进行宽度/高度检查.如果你想一下,这正是你想要的 - 知道宽度是否小于高度(肖像),相反(横向)或它们是否相同(正方形).
然后它归结为这个简单的代码:
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;
}
Run Code Online (Sandbox Code Playgroud)
Pau*_*aul 52
解决此问题的另一种方法是不依赖于显示器的正确返回值,而是依赖于Android资源解析.
在文件layouts.xml夹中创建文件res/values-land并res/values-port使用以下内容:
RES /价值观土地/ layouts.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="is_landscape">true</bool>
</resources>
Run Code Online (Sandbox Code Playgroud)
RES /值端口/ layouts.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="is_landscape">false</bool>
</resources>
Run Code Online (Sandbox Code Playgroud)
在源代码中,您现在可以按如下方式访问当前方向:
context.getResources().getBoolean(R.bool.is_landscape)
Run Code Online (Sandbox Code Playgroud)
Ngu*_*inh 46
一种指定手机当前方向的完整方式:
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";
}
}
Run Code Online (Sandbox Code Playgroud)
Chear Binh Nguyen
Ngu*_*Dat 27
以下是hackbod和Martijn推荐的代码片段演示如何获取屏幕方向:
❶更改时触发方向:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
int nCurrentOrientation = _getScreenOrientation();
_doSomeThingWhenChangeOrientation(nCurrentOrientation);
}
Run Code Online (Sandbox Code Playgroud)
❷获取hackbod推荐的当前方向:
private int _getScreenOrientation(){
return getResources().getConfiguration().orientation;
}
Run Code Online (Sandbox Code Playgroud)
❸有获得当前屏幕方向的替代解决方案❷遵循Martijn解决方案:
private int _getScreenOrientation(){
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
return display.getOrientation();
}
Run Code Online (Sandbox Code Playgroud)
★ 注意:我试过两个工具❷&❸,但在RealDevice(NexusOne SDK 2.3)方向上,它返回错误的方向.
★所以我建议使用解决方案 ❷来获得更多优势的屏幕方向:清晰,简单,工作就像一个魅力.
★仔细检查方向的返回,以确保我们的预期正确(可能有限取决于物理设备规格)
希望它有所帮助,
小智 16
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;
}
Run Code Online (Sandbox Code Playgroud)
net*_*ein 13
使用getResources().getConfiguration().orientation它是正确的方式.
您只需要注意不同类型的景观,即设备通常使用的景观和其他景观.
仍然不明白如何管理.
Baz*_*Baz 12
一段时间过去了,因为大多数答案已经发布,有些时候已经弃用了方法和常量.
我已经更新了Jarek的代码,不再使用这些方法和常量了:
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;
}
Run Code Online (Sandbox Code Playgroud)
请注意,Configuration.ORIENTATION_SQUARE不再支持该模式.
我发现这在我测试过的所有设备上都是可靠的,与建议使用的方法形成鲜明对比 getResources().getConfiguration().orientation
在运行时检查屏幕方向.
@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();
}
}
Run Code Online (Sandbox Code Playgroud)
只需简单的两行代码
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
// do something in landscape
} else {
//do in potrait
}
Run Code Online (Sandbox Code Playgroud)
还有一种方法:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
Android SDK 可以很好地告诉您这一点:
getResources().getConfiguration().orientation
Run Code Online (Sandbox Code Playgroud)
小智 5
这样就覆盖了所有手机,例如oneplus3
public static boolean isScreenOrientationPortrait(Context context) {
return context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
}
Run Code Online (Sandbox Code Playgroud)
正确代码如下:
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;
}
Run Code Online (Sandbox Code Playgroud)
2019 年在 API 28 上进行了测试,无论用户是否设置了纵向方向,与另一个过时的答案相比,使用最少的代码,以下内容提供了正确的方向:
/** @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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
253012 次 |
| 最近记录: |