프로그래밍/Android

화면 회전(rotation) 처리

우진샘 2013. 8. 9. 12:36

화면 회전(rotation) 처리

안드로이드는 기본적으로 화면 회전에 대한 처리를 자동으로 해주고 있다.
하지만 Activity를 종료하고 다시 실행 시키는 방식이기 때문에 onCreate 함수가 매번 불리게 되며 데이터가 유지 되지 않는 문제가 있다.


* 안드로이드가 화면 처리를 하고 데이터만 유지하는 방법

asfasdfasdfa


* 사용자가 직접 화면 처리를 하는 방법

 1. AndroidManifest.xml의 activity에 다음 항목을 추가

android:configChanges="keyboardHidden|orientation|screenSize"

   - 만약 screenSize가 빠질 경우 onCreate 함수가 불려지니 주의


 2. onConfigurationChanged 함수 오버라이드

if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { // 세로 전환시 발생
    setContentView(R.layout.XXXX_vertical);        // 세로 방향에서 사용할 레이아웃 
} else if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { // 가로 전환시 발생
    setContentView(R.layout.XXXX_horizontal);    // 가로 방향에서 사용할 레이아웃
}


 3. onCreate 함수 오버라이드

if( this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
    setContentView(R.layout.XXXX_vertical);        // 세로 방향에서 사용할 레이아웃
} else {
    setContentView(R.layout.XXXX_horizontal);    // 가로 방향에서 사용할 레이아웃
}


* 화면을 고정하는 방법

AndroidManifest.xml의 activity에 다음 항목을 추가

android:screenOrientation="portrait"        // 세로 모드

android:screenOrientation="landscape"   // 가로 모드