3 回答

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超7個(gè)贊
我嘗試了kagronick的答案,但無(wú)法正常工作。經(jīng)過(guò)一番混亂之后,我最終使用系統(tǒng)覆蓋窗口使其工作并刪除了我發(fā)現(xiàn)不必要的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);
您可以在基于此發(fā)布的應(yīng)用程序中看到我的成功:強(qiáng)制??啃D(zhuǎn)。

TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超9個(gè)贊
這可以通過(guò)創(chuàng)建一個(gè)隱藏的系統(tǒng)對(duì)話框來(lái)完成。有點(diǎn)像駭客,但足以瘋狂地工作了。
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);

TA貢獻(xiàn)1852條經(jīng)驗(yàn) 獲得超1個(gè)贊
順便說(shuō)一句,您還可以全局更改屏幕方向,而不必在不支持的應(yīng)用程序中強(qiáng)制屏幕方向。(我知道這個(gè)問(wèn)題是關(guān)于覆蓋應(yīng)用程序的首選項(xiàng),但這仍然有用,并且大多數(shù)都是相關(guān)信息。)
授予對(duì)AndroidManifest.xml中的系統(tǒng)設(shè)置(WRITE_SETTINGS)的訪問(wèn)權(quán)限
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
進(jìn)口 Settings
import android.provider.Settings;
確保禁用了Android屏幕自動(dòng)旋轉(zhuǎn)
Settings.System.putInt(
getContentResolver(),
Settings.System.ACCELEROMETER_ROTATION,
0
);
設(shè)置USER_ROTATION為所需的設(shè)置,該設(shè)置應(yīng)為ROTATION_常量之一。這些值代表設(shè)備自然方向的屏幕旋轉(zhuǎn),可以是橫向或縱向。
Settings.System.putInt(
getContentResolver(),
Settings.System.USER_ROTATION,
Surface.ROTATION_0 //Or a different ROTATION_ constant
);
請(qǐng)注意,USER_ROTATION即使您的應(yīng)用未運(yùn)行或已卸載,更改也將保留。如果我沒(méi)記錯(cuò)的話,用戶可以通過(guò)手動(dòng)禁用和啟用自動(dòng)旋轉(zhuǎn)來(lái)重置此值。
- 3 回答
- 0 關(guān)注
- 909 瀏覽
添加回答
舉報(bào)