![](/img/trans.png)
[英]How to lock screen orientation in reverse portrait and reverse landscape mode
[英]How to Lock screen orientation when displaying reverse landscape in android?
我嘗試鎖定屏幕orientation
Android中application.I二手下面的代碼,以鎖定屏幕方向,當特定按鈕點擊火災。
// Inside button click
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
if (display.getOrientation() == 1) {
setRequestedOrientation(0);
} else if (display.getOrientation() == 0) {
setRequestedOrientation(1);
} else if (display.getOrientation() == 3) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
}
上面的代碼工作橫向和縱向屏幕方向,但它不工作反向景觀mode.In這種情況下,始終我的活動改變它的orientation
,以默認landscape
mode.Also我注意到,當裝置反向風景模式, display.getOrientation()
總是返回 3。
我怎樣才能找到這個問題的解決方案?
我一直在使用 API 8 處理同樣的問題。看起來在 reserseLandscape 模式下,如果你調用
getResources().getConfiguration().orientation
Android 將返回 2 (SCREEN_ORIENTATION_USER),它根本不會鎖定方向。 實際上,即使該值確實存在,也沒有為 ActivityInfo 類定義常量 SCREEN_ORIENTATION_REVERSE_LANDSCAPE。 對於我的應用程序,我最終創建了這個方法:
public void lockScreenOrientation() {
switch (((WindowManager) getSystemService(WINDOW_SERVICE))
.getDefaultDisplay().getRotation()) {
case Surface.ROTATION_90:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
case Surface.ROTATION_180:
setRequestedOrientation(9/* reversePortait */);
break;
case Surface.ROTATION_270:
setRequestedOrientation(8/* reverseLandscape */);
break;
default :
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
每當我需要鎖定方向時我都會調用這個方法,然后我可以簡單地釋放它調用
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
希望能幫助到你。
public void lockScreenOrientation() {
switch (((WindowManager) getSystemService(WINDOW_SERVICE))
.getDefaultDisplay().getRotation()) {
case Surface.ROTATION_90:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
case Surface.ROTATION_180:
setRequestedOrientation(9/* reversePortait */);
break;
case Surface.ROTATION_270:
setRequestedOrientation(8/* reverseLandscape */);
break;
default :
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
autremoi 的答案對我有用,我創建了一個新答案,因為我的聲譽還沒有讓我投票給他。
我的代碼唯一的區別是我用另一個恢復了傳感器
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
在您的 AndroidManifest 文件中,只需輸入以下代碼:
android:configChanges="keyboard|orientation|keyboardHidden|screenSize"
android:screenOrientation="reverseLandscape"
例子:
<activity
android:name="com.gaspar.slinfilipino.Quiz"
android:label="@string/title_activity_quiz"
android:configChanges="keyboard|orientation|keyboardHidden|screenSize"
android:screenOrientation="reverseLandscape"
android:parentActivityName="com.gaspar.slinfilipino.SignLanguageMenu" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.gaspar.slinfilipino.SignLanguageMenu" />
</activity>
它對我有效。 希望能幫助到你!
我嘗試了 autremoi 的答案,但它殺死了我的用戶界面。 我找到了一個確實有效的答案,但是我發現它是混合的: https : //www.captechconsulting.com/blogs/programmatically-locking-android-screen-orientation
而這個: getSize() 給我錯誤的作品,並且應該在所有 Android 版本上。
與setRequestedOrientation(lockOrientation());
快樂編碼!
private int lockOrientation(){
Display display = getWindowManager().getDefaultDisplay();
int rotation = display.getRotation();
Point size = new Point();
size.x = display.getWidth();
size.y = display.getHeight();
int lock = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
if (rotation == Surface.ROTATION_0
|| rotation == Surface.ROTATION_180) {
// if rotation is 0 or 180 and width is greater than height, we have
// a tablet
if (size.x > size.y) {
if (rotation == Surface.ROTATION_0) {
lock = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
} else {
lock = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
}
} else {
// we have a phone
if (rotation == Surface.ROTATION_0) {
lock = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
} else {
lock = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
}
}
} else {
// if rotation is 90 or 270 and width is greater than height, we
// have a phone
if (size.x > size.y) {
if (rotation == Surface.ROTATION_90) {
lock = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
} else {
lock = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
}
} else {
// we have a tablet
if (rotation == Surface.ROTATION_90) {
lock = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
} else {
lock = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
}
}
}
return lock;
}
嗨,如果你想要鎖定屏幕方向,你必須設置清單文件
<activity android:configChanges="orientation|keyboardHidden"
android:screenOrientation="portrait" android:name=".activities.HomeActivity"
android:label="@string/app_name" android:theme="@style/Theme.Translucent">
嘗試這個
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
如果你想要肖像
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.