안드로이드 RotationTestApp 프로젝트

Jmnote (토론 | 기여)님의 2013년 1월 4일 (금) 01:02 판 (→‎같이 보기)
안드로이드 RotationTestApp

1 새 프로젝트 생성

  • File --- New --- Android Application Project
  • "New Android Application" --- Application Name: Rotation Test App[1] --- [Next >]
  • "Configure Project" --- [Next >]
  • "Configure Launcher Icon" --- [Next >]
  • "Create Activity"[2] --- [Next >]
  • "New Blank Activity" --- [Finish]

여기까지 하면 왼쪽 Package Explorer에 RotationTestApp이라는 프로젝트가 생기고 activity_main.xml이 열린다. 화면에는 앱 이름 Rotation Test App과 함께 Hello world! 도 찍혀 있다...

2 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;
		}  		
	}
}

3 같이 보기

4 주석

  1. 앱 이름만 입력하면 프로젝트명과 패키지명은 자동으로 채워진다.
  2. BlankActivity가 기본값
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}