我有一项活动.此活动适用于纵向模式或横向模式.在横向模式下,我加载一个新布局以显示图形.为了检测方向,我在清单中设置了这个:
android:screenOrientation="sensor" android:configChanges="orientation"
Run Code Online (Sandbox Code Playgroud)
它工作得很好,因为当我翻转手机时,会加载新的布局.但是,在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);
}
Run Code Online (Sandbox Code Playgroud)
此代码有效,但当用户处于横向模式并将手机翻转为纵向模式且锁定打开时,lanscape布局将以纵向模式翻转,我不希望这样!
我已经添加
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Run Code Online (Sandbox Code Playgroud)
但此代码禁用onConfigurationChanged方法,我无法检测用户是否再次翻转手机...
你能帮助我吗?
在 android:configChanges 中进行更改:-
android:configChanges="orientation|screenSize|keyboard|keyboardHidden"
Run Code Online (Sandbox Code Playgroud)
更改后你的代码应该可以工作