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

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


这是一个能够覆盖应用程序所请求方向的应用程序之一:


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


回首忆惘然
浏览 840回答 3
3回答

qq_笑_17

我尝试了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_WINDOWWindowManager.LayoutParams orientationLayout = new WindowManager.LayoutParams(WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, 0, PixelFormat.RGBA_8888);// Use whatever constant you need for your desired rotationorientationLayout.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR;WindowManager wm = (WindowManager) this.getSystemService(Service.WINDOW_SERVICE);wm.addView(orientationChanger, orientationLayout);orientationChanger.setVisibility(View.VISIBLE);您可以在基于此发布的应用程序中看到我的成功:强制停靠旋转。

暮色呼如

这可以通过创建一个隐藏的系统对话框来完成。有点像骇客,但足以疯狂地工作了。    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);

小怪兽爱吃肉

顺便说一句,您还可以全局更改屏幕方向,而不必在不支持的应用程序中强制屏幕方向。(我知道这个问题是关于覆盖应用程序的首选项,但这仍然有用,并且大多数都是相关信息。)授予对AndroidManifest.xml中的系统设置(WRITE_SETTINGS)的访问权限<uses-permission android:name="android.permission.WRITE_SETTINGS" />进口 Settingsimport android.provider.Settings;确保禁用了Android屏幕自动旋转Settings.System.putInt(&nbsp; &nbsp; getContentResolver(),&nbsp; &nbsp; Settings.System.ACCELEROMETER_ROTATION,&nbsp; &nbsp; 0);设置USER_ROTATION为所需的设置,该设置应为ROTATION_常量之一。这些值代表设备自然方向的屏幕旋转,可以是横向或纵向。Settings.System.putInt(&nbsp; &nbsp; getContentResolver(),&nbsp; &nbsp; Settings.System.USER_ROTATION,&nbsp; &nbsp; Surface.ROTATION_0 //Or a different ROTATION_ constant);请注意,USER_ROTATION即使您的应用未运行或已卸载,更改也将保留。如果我没记错的话,用户可以通过手动禁用和启用自动旋转来重置此值。
打开App,查看更多内容
随时随地看视频慕课网APP