想要在螢幕旋轉的時候,做一些操作,請參考

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

    // Checks the orientation of the screen

    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
    }
}

同時,如果 targetSdkVersion >= 13, 需要在 AndroidManifest.xml 針對此 Activity 加入:

android:configChanges="orientation|screenSize"

另外,如果你有使用 layout/layout-land/ 來切換不同的 layout view, 那請記得在 onConfigurationChanged() 中還要 setContentView(layoutResId);:

@Override public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);                
            setContentView(R.layout.main);
    ...
    ...
    ...

    }

其它補充

Comments

comments powered by Disqus
Copyright © 2013 Andro Chen
Powered by Logdown and Greyshade
Favicon from The Noun Project