禁用定向但检测方向 [英] Disable orientation but detect orientation

查看:186
本文介绍了禁用定向但检测方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动。这项活动工作在肖像模式和横向模式。在横向模式下,我打开一个新的布局,显示的图表。
为了检测姿态,我成立了这个在我的清单:

I've got an activity. This activity works in portrait mode or landscape mode. In landscape mode, I load a new layout to show a graph. To detect the orientation, I set up this in my manifest :

android:screenOrientation="sensor" android:configChanges="orientation"

它完美,因为当我翻我的手机,新的布局被加载。但是,在lanscape模式下,用户可以禁止/允许一个按钮(这是一个切换器)的旋转。

It works perfectly, because when I flip my phone, the new layout is loaded. But, in lanscape mode, the user can disable/enable the rotation with a button (it's a switcher).

我写这篇code:

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);
}

这code工作,但是当用户在横向模式和翻转手机早在肖像模式和锁开启时,lanscape布局在纵向模式下翻转,我不希望出现这种情况!

This code works, but when the user is in landscape mode and flips the phone to back in portrait mode and the lock is on, the lanscape layout flips in portrait mode and I don't want that!

我添加

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

但这code禁用onConfigurationChanged方法,如果用户再次翻转电话我不能检测...

but this code disables the onConfigurationChanged method and I can't detect if the user flips the phone again...

你能帮助我吗?

推荐答案

请在Android的变化:configChanges: -

make changes in android:configChanges:-

  android:configChanges="orientation|screenSize|keyboard|keyboardHidden"

更改后您code应该工作

after changes you code should be work

这篇关于禁用定向但检测方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆