android wrt Orientation是否存在问题

时间:2013-03-19 04:37:50

标签: android orientation android-orientation

似乎Android设备上的方向更改存在问题。当我导航到设置 - 应用程序 - 运行和我选择 - 显示缓存的进程,然后翻转它更改回的方向 - 显示正在运行的进程。我在我的应用程序中有一个用例在哪里我采用设备方向来显示一些选项,但这似乎并没有得到尊重。对此有任何建议,可能是因为我提到的样本流程? 我用这个来获得方向:

getResources().getConfiguration().orientation

这用于轮换:

Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int rotation = display.getRotation();

1 个答案:

答案 0 :(得分:1)

你使用的是正确的,我使用两个组合

int orientation = getResources().getConfiguration().orientation;
int rotation = getWindowManager().getDefaultDisplay().getOrientation();

现在使用它们来确定正确的方向

    if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90) {
            if (orientation == Configuration.ORIENTATION_PORTRAIT) {
                // Portrait 
            }
            else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
                // Landscape
            }
        }
        else if (rotation == Surface.ROTATION_180 || rotation == Surface.ROTATION_270) {
            if (orientation == Configuration.ORIENTATION_PORTRAIT) {
                //Mainly for tablets
            }
            else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
               //Mainly for tablets
            }
        }