我试图在Android应用程序中锁定屏幕orientation
。当特定按钮单击时,我使用以下代码锁定屏幕方向。
// 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);
}
以上代码适用于横向和纵向屏幕方向,但不适用于反向横向模式。在这种情况下,我的活动总是将其orientation
更改为默认landscape
模式。此外,我注意到,当设备在反向横向模式下,display.getOrientation()
始终返回3.
如何找到此问题的解决方案?
答案 0 :(得分:15)
我一直在使用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);
希望它有所帮助。
答案 1 :(得分:3)
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);
答案 2 :(得分:0)
在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>
它适用于我。希望它有所帮助!
答案 3 :(得分:0)
我尝试了autremoi的答案,但它杀死了我的UI。我找到了一个确实有效的答案,但是我发现了这个问题的混合:http://www.captechconsulting.com/blog/eric-miles/programmatically-locking-android-screen-orientation
并且:getSize() giving me errors有效,并且应该适用于所有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;
}
答案 4 :(得分:-1)
嗨如果您想要锁定屏幕方向,则必须设置清单文件
<activity android:configChanges="orientation|keyboardHidden"
android:screenOrientation="portrait" android:name=".activities.HomeActivity"
android:label="@string/app_name" android:theme="@style/Theme.Translucent">
答案 5 :(得分:-1)
试试这个
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
如果你想要肖像
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);