- 안드로이드 RotationTestApp
개요
- 주레이아웃(activity_main)에 2개의 RelativeLayout를 추가
- 회전 이벤트 발생시, 화면영역의 높이와 너비를 구하여 현재 방향이 가로인지 세로인지 파악하고[1] 두 RelativeLayout을 보기 좋게 재배치
새 프로젝트 생성
- File --- New --- Android Application Project
- "New Android Application" --- Application Name: Rotation Test App[2] --- [Next >]
- "Configure Project" --- [Next >]
- "Configure Launcher Icon" --- [Next >]
- "Create Activity"[3] --- [Next >]
- "New Blank Activity" --- [Finish]
여기까지 하면 왼쪽 Package Explorer에 RotationTestApp이라는 프로젝트가 생기고 activity_main.xml이 열린다. 화면에는 앱 이름 Rotation Test App과 함께 Hello world! 도 찍혀 있다...
AndroidManifest.xml 수정
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.rotationtestapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16"
tools:ignore="OldTargetApi"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.rotationtestapp.MainActivity"
android:configChanges="orientation|screenSize"
android:label="@string/app_name"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml
- RotationTestApp > res > layout > activity_main.xml
<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:background="#FFF" >
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#F00" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Button" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/relativeLayout2"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#00F" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Text" />
</RelativeLayout>
</RelativeLayout>
MainActivity.java 수정
package com.example.rotationtestapp;
import android.os.Bundle;
import android.app.Activity;
import android.content.res.Configuration;
import android.graphics.Point;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
private RelativeLayout layout1;
private RelativeLayout layout2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout1 = (RelativeLayout) findViewById(R.id.relativeLayout1);
layout2 = (RelativeLayout) findViewById(R.id.relativeLayout2);
AdjustLayout();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
AdjustLayout();
}
private void AdjustLayout() {
Point point = new Point();
getWindowManager().getDefaultDisplay().getSize(point);
boolean is_landscape = point.x > point.y;
RelativeLayout.LayoutParams layparam1 = (RelativeLayout.LayoutParams) layout1.getLayoutParams();
RelativeLayout.LayoutParams layparam2 = (RelativeLayout.LayoutParams) layout2.getLayoutParams();
if(is_landscape) {
layparam1.width = point.y;
layparam1.height = point.y;
layparam1.leftMargin = point.x - point.y;
layparam1.topMargin = 0;
layparam2.width = point.x - point.y;
layparam2.height = point.y;
layparam2.leftMargin = 0;
layparam2.topMargin = 0;
}
else {
layparam1.width = point.x;
layparam1.height = point.x;
layparam1.leftMargin = 0;
layparam1.topMargin = point.y - point.x;
layparam2.width = point.x;
layparam2.height = point.y - point.x;
layparam2.leftMargin = 0;
layparam2.topMargin = 0;
}
}
}