相关疑难解决方法(0)

屏幕方向锁定

是否有可靠的方法来锁定所有Android设备上的屏幕方向?以下代码适用于我的Nexus S和其他手机,但出于某种原因,ROTATION_90对应于Xoom上的SCREEN_ORIENTATION_REVERSE_PORTRAIT.

有没有办法可靠地将旋转映射到方向?

private void lockScreenOrientation() {
    if (!mScreenOrientationLocked) {
        final int orientation = getResources().getConfiguration().orientation;
        final int rotation = getWindowManager().getDefaultDisplay().getOrientation();

        if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90) {
            if (orientation == Configuration.ORIENTATION_PORTRAIT) {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            }
            else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            }
        }
        else if (rotation == Surface.ROTATION_180 || rotation == Surface.ROTATION_270) {
            if (orientation == Configuration.ORIENTATION_PORTRAIT) {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
            }
            else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
            }
        }

        mScreenOrientationLocked = true;
    }
}

private void unlockScreenOrientation() { …
Run Code Online (Sandbox Code Playgroud)

android locking screen rotation orientation

34
推荐指数
4
解决办法
4万
查看次数

标签 统计

android ×1

locking ×1

orientation ×1

rotation ×1

screen ×1