如何在android 2.3.3中使用onConfigurationChanged()和newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE

Raj*_*esh 35 android

我在用onConfigurationChanged().在那里,当我从LandScape改为Portrait时,它正在调用if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)并从LandScape转移到Portrait.但是当我从Portrait改为Land-Scape时,它并没有改变为LandScape,因为它正在调用if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)它,它并没有从LandScape变为Portrait.请帮忙.

public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        //int orientation = this.getResources().getConfiguration().orientation;

        if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            Log.d("Entered to change as Portrait ","PPPPPPPPPPPPPPPPP");
            setContentView(R.layout.settings);
        } else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            Log.d("Entered to change as LandScape ","LLLLLLLLLLLLLLLLLLLL");
            setContentView(R.layout.settings);
        }

    }
Run Code Online (Sandbox Code Playgroud)

Aja*_*jay 46

只需将以下代码写入onConfigurationChanged方法和测试即可

if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){

    Log.e("On Config Change","LANDSCAPE");
}else{

    Log.e("On Config Change","PORTRAIT");
}
Run Code Online (Sandbox Code Playgroud)

并写入这样的最android:configChanges="keyboardHidden|orientation"明显的文件

<activity android:name="TestActivity"
           android:configChanges="keyboardHidden|orientation">
Run Code Online (Sandbox Code Playgroud)

它在我身边工作,我希望它可以帮助你.

如果你对平板电脑也加入|screenSizeandroid:configChanges

  • 如果你在平板电脑上也添加`| screenSize`到android:configChanges (12认同)

Luk*_*cek 16

你还应该知道:

注意:从Android 3.2(API级别13)开始,当设备在纵向和横向之间切换时,"屏幕大小"也会更改.因此,如果要在开发API级别13或更高级别(由minSdkVersion和targetSdkVersion属性声明)时由于方向更改而阻止运行时重新启动,则除了"orientation"值之外,还必须包含"screenSize"值.也就是说,你必须decalare android:configChanges ="orientation | screenSize".但是,如果您的应用程序的目标是API级别12或更低,那么您的活动始终会自行处理此配置更改(即使在Android 3.2或更高版本的设备上运行,此配置更改也不会重新启动您的活动).