티스토리 뷰

Android Landscape 


protrait :세로 고정

landscape : 가로 고정

sensorPortrait : 세로 고정, 센서에 따라 정/역방향 화면 전환

sensorLandscape : 가로 고정, 센서에 따라 정/역방향 화면 전환

sensor : 센서에 따라 4가지 방향으로 회전


코드로 액티비티의 방향전환을 컨트롤 하려면 onConfigurationChanged 메소드를 통해 방향이 바뀌었을 때의 처리를 한다.



XML로 레이아웃을 관리하는 예제를 실습해보았다.


xml 파일 생성, 수정

먼저 프로젝트를 생성하면 activity_main이 생성된다. 

이것 이외에 가로방향일 경우의 리소스 파일을 생성해야한다.

res우클릭하여 new resource file을 선택한다.

이름은 activity_main 이지만 Directory name은 layout-land 라고 입력한다.




기존의 activity_main.xml 에 EditText를 생성하였다.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.ktds.jmj.mylandscapelayout.MainActivity">

<EditText
android:id="@+id/etText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>




activity_main(land).xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ScrollView
android:layout_width="200dp"
android:layout_height="match_parent">

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">


<TextView
android:text="Menu 영역"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>
</ScrollView>

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:text="본문 영역"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<EditText
android:id="@+id/etText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>

</ScrollView>

</LinearLayout>



Manifest 수정

xml을 수정하고 Manifest에서 화면변경에 대한 속성을 추가해야한다.


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ktds.jmj.mylandscapelayout">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:configChanges="screenSize|orientation"
android:screenOrientation="sensor" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>



configChanges 는 화면전환시 자동으로 onDestory()와 onCreate()를 호출하지 않고 onConfigurationChanged() 메소드를 호출하여 내가 원하는 내용으로 환경설정을 하는 것이다.

screenOrientation 은 레이아웃을 어떻게 변경시킬건지 결정한다.




MainActivity 수정


MainActivity.java

MainActivity에서는 화면을 전환했을 때에도 입력한 데이터를 그대로 유지할 수 있도록 코드를 짜보았다.

package com.ktds.jmj.mylandscapelayout;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

private EditText etText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

etText = (EditText) findViewById(R.id.etText);
}

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

String text = etText.getText().toString();
setContentView(R.layout.activity_main);

etText = (EditText) findViewById(R.id.etText);
etText.setText(text);
}
}



결과화면






댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함