为了账号安全,请及时绑定邮箱和手机立即绑定

如何在Android中全局强制屏幕方向?

如何在Android中全局强制屏幕方向?

回首忆惘然 2019-11-30 14:10:09
有许多应用程序可以强制屏幕旋转。即使某个应用明确希望以其他方向查看,它们也可以工作。现在,我禁用了加速度计旋转系统设置,并设置了我的首选方向。应用仍可以覆盖此设置。这是一个能够覆盖应用程序所请求方向的应用程序之一:https://play.google.com/store/apps/details?id=nl.fameit.rotate&hl=zh_CN
查看完整描述

3 回答

?
qq_笑_17

TA贡献1818条经验 获得超7个赞

我尝试了kagronick的答案,但无法正常工作。经过一番混乱之后,我最终使用系统覆盖窗口使其工作并删除了我发现不必要的LayoutParam。这是我最终的解决方案:


orientationChanger = new LinearLayout(this);

// Using TYPE_SYSTEM_OVERLAY is crucial to make your window appear on top

// You'll need the permission android.permission.SYSTEM_ALERT_WINDOW

WindowManager.LayoutParams orientationLayout = new WindowManager.LayoutParams(WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, 0, PixelFormat.RGBA_8888);

// Use whatever constant you need for your desired rotation

orientationLayout.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR;


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

wm.addView(orientationChanger, orientationLayout);

orientationChanger.setVisibility(View.VISIBLE);

您可以在基于此发布的应用程序中看到我的成功:强制停靠旋转。


查看完整回答
反对 回复 2019-11-30
?
暮色呼如

TA贡献1853条经验 获得超9个赞

这可以通过创建一个隐藏的系统对话框来完成。有点像骇客,但足以疯狂地工作了。


    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);


查看完整回答
反对 回复 2019-11-30
?
小怪兽爱吃肉

TA贡献1852条经验 获得超1个赞

顺便说一句,您还可以全局更改屏幕方向,而不必在不支持的应用程序中强制屏幕方向。(我知道这个问题是关于覆盖应用程序的首选项,但这仍然有用,并且大多数都是相关信息。)


授予对AndroidManifest.xml中的系统设置(WRITE_SETTINGS)的访问权限


<uses-permission android:name="android.permission.WRITE_SETTINGS" />

进口 Settings


import android.provider.Settings;

确保禁用了Android屏幕自动旋转


Settings.System.putInt(

    getContentResolver(),

    Settings.System.ACCELEROMETER_ROTATION,

    0

);

设置USER_ROTATION为所需的设置,该设置应为ROTATION_常量之一。这些值代表设备自然方向的屏幕旋转,可以是横向或纵向。


Settings.System.putInt(

    getContentResolver(),

    Settings.System.USER_ROTATION,

    Surface.ROTATION_0 //Or a different ROTATION_ constant

);

请注意,USER_ROTATION即使您的应用未运行或已卸载,更改也将保留。如果我没记错的话,用户可以通过手动禁用和启用自动旋转来重置此值。


查看完整回答
反对 回复 2019-11-30
  • 3 回答
  • 0 关注
  • 835 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信