如何在 Android 中全局强制屏幕方向? [英] How can I globally force screen orientation in Android?

查看:35
本文介绍了如何在 Android 中全局强制屏幕方向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有许多应用程序可以强制屏幕旋转.即使应用程序明确希望以其他方向查看,它们也能正常工作.现在我正在禁用加速度计旋转系统设置并设置我的首选方向.应用仍然可以覆盖它.

There are a number of apps that are able to force screen rotation. They work even if an app explicitly wants to be viewed in another orientation. Right now I am disabling the accelerometer rotation system setting and setting my preferred orientation. An app can still override this.

以下是能够覆盖应用请求方向的应用之一:

Here is one of the apps that is able to override an app's requested orientation:

https://play.google.com/store/apps/details?id=nl.fameit.rotate&hl=en

推荐答案

这可以通过创建一个隐藏的系统对话框来完成.它有点像黑客,但它足够疯狂,可以工作.

This can be done by creating a hidden system dialog. Its kind of a hack but its crazy enough to work.

    wm = (WindowManager) content.getSystemService(Service.WINDOW_SERVICE);

    orientationChanger = new LinearLayout(content);
    orientationChanger.setClickable(false);
    orientationChanger.setFocusable(false);
    orientationChanger.setFocusableInTouchMode(false);
    orientationChanger.setLongClickable(false);

    orientationLayout = new WindowManager.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
            windowType, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                    | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.RGBA_8888);

    wm.addView(orientationChanger, orientationLayout);
    orientationChanger.setVisibility(View.GONE);

    orientationLayout.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
    wm.updateViewLayout(orientationChanger, orientationLayout);
    orientationChanger.setVisibility(View.VISIBLE);

这篇关于如何在 Android 中全局强制屏幕方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆