我有个活动要做。此活动在纵向模式或横向模式下工作。在横向模式下,我加载一个新的布局来显示图形。为了检测方向,我在清单中设置了以下内容:
android:screenOrientation="sensor" android:configChanges="orientation"它工作得很好,因为当我翻转我的手机时,新的布局被加载了。但是,在lanscape模式下,用户可以通过一个按钮(它是一个切换器)来禁用/启用旋转。
我写了这段代码:
public void onConfigurationChanged(Configuration pNewConfig) {
Log.v(DEBUG_TAG, "Current configuration changes");
if (pNewConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Log.v(DEBUG_TAG, "Screen orientation : LANDSCAPE");
if (pNewConfig.orientation != mCurrentOrientation) {
initTestLandscape();
mCurrentOrientation = pNewConfig.orientation;
}
} else {
Log.v(DEBUG_TAG, "Screen orientation : PORTRAIT");
if (!mIsLandscapeLocking
&& pNewConfig.orientation != mCurrentOrientation) {
initTestPortrait();
mCurrentOrientation = pNewConfig.orientation;
}
}
super.onConfigurationChanged(pNewConfig);
}这段代码可以工作,但当用户在横向模式,翻转手机回到肖像模式,锁是打开的,lanscape布局翻转到纵向模式,我不想这样!
我已经添加了
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);但是这段代码禁用了onConfigurationChanged方法,并且我无法检测用户是否再次翻转手机……
你能帮帮我吗?
发布于 2015-06-16 22:09:02
在android:configChanges中进行更改:-
android:configChanges="orientation|screenSize|keyboard|keyboardHidden"更改后,您的代码应该是有效的
https://stackoverflow.com/questions/7137499
复制相似问题