[1] Android 개요
1. 4대 구성요소
- Activity : 화면
- Service : 화면에 보이지 않는 부분
- Broadcast Receiver : action이 발생했을 때 실행되는 것
- Content Provider : 데이터 제공
2. 프로젝트 구조
- 프로젝트 보는 방식을 Android로 설정하면, Android App 개발을 하기 편한 방식으로 구조를 보여준다.
- Project로 설정하면, 실제로 컴퓨터에 저장된 형태로 파일을 보여주지만, 개발용으로는 불편하다.
1) app
① manifests
- AndroidManifest.xml 파일이 있으며, 설정파일
② java
- com.example.프로젝트명 패키지 내에 실제 소스 파일들을 구성한다.
③ res
- 리소스 파일을 모아놓은 곳(주로 정적인 파일, 웹에서 static과 유사)
- default로 만들어지는 것들 외 여러가지가 들어갈 수 있다.
- drawable : 그림 관련 파일
- layout : 화면 구성 관련 파일(웹에서 html파일을 모아놓는 templates 폴더와 유사)
- mipmap : 앱 아이콘 파일
- values : 여러가지 값들을 저장해 놓은 파일, 대표적으로 색깔, 문자열을 저장한 파일들이 있다.
2) Gradle Scripts
[2] Activity
1. 생성
- com.example.프로젝트명 폴더 오른쪽 버튼 클릭 -> New -> Activity -> Empty Activity
- Activity Name 을 설정 (Layout Name이 자동으로 activity_설정한 이름 으로 설정된다.)하고 Finish 클릭
- AndroidManifest.xml 파일을 열어보면 activity가 추가된 것을 볼 수 있다.
- <indent-filter> 부분이 있어야 App 실행했을 때 뜨는 화면으로 설정된다.
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
2. Lifecycle
- 화면의 lifecycle은 다음과 사진과 같다.
- lifecycle 확인 예제
LifeCycleTestActivity.kt
package com.example.firstpro
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
// Lifecycle
/*
액티비티 실행
onCreate -> onStart -> onResume
일시정지(액티비티 화면이 실행 중이 아닌 경우 - 현재 액티비티 위에 다른 액티비티가 있는 경우)
onPause -> onStop
일시정지해제(현재 작업 중인 액티비티가 화면 맨 위에 있는 경우 - foreground로 이동)
onRestart -> onStart -> onResume
앱이 종료
onPause -> onStop -> onDestroy
*/
class LifeCycleTestActivity : AppCompatActivity() {
// Activity가 생성될 때 자동으로 호출 - 1
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// 레이아웃리소스를 해석하고 실행해서 화면에 배치하라는 의미
setContentView(R.layout.activity_life_cycle_test)
Log.d("lifecycle","onCreate()호출..")
}
// onCreate 다음으로 호출되는 메소드 - 2
// 액티비티가 활성화 될 때 다시 호출 - UI 관련 코드를 초기화
override fun onStart() {
super.onStart()
Log.d("lifecycle","onStart()호출..")
}
// onStart 다음으로 호출되는 메소드 - 3
// 리소스에 대한 초기화 작업 시작, 액티비티가 활성화된 상태에서 실행하는 모든 기능을 초기화하고 활성화
override fun onResume() {
super.onResume()
Log.d("lifecycle","onResume()호출..")
}
override fun onRestart() {
super.onRestart()
Log.d("lifecycle","onRestart()호출..")
}
// 일시정지 상태로 바뀔 때 호출되는 메소드
// 액티비티가 비활성화될 때(맨 앞에 있지 않은 경우) 제일 먼저 호출되는 메소드
// - 배터리 수명에 영향을 줄 수 있는 실행흐름을 중지하거나 리소스의 해제 등을 해야 한다.
// - 필요없는 기능 중지
override fun onPause() {
super.onPause()
Log.d("lifecycle","onPause()호출..")
}
// 화면이 더 이상 보이지 않는 경우
// - 앱이 실행되지 않는 동안 사용하지 않는 리소스를 해제
// - 작업이 완료된 시점에 데이터베이스에 저장하기
override fun onStop() {
super.onStop()
Log.d("lifecycle","onStop()호출..")
}
// 활동이 중지되는 경우(앱이 중지되는 경우) - 해제되지 않은 리소스를 해제
override fun onDestroy() {
super.onDestroy()
Log.d("lifecycle","onDestroy()호출..")
}
// 버튼을 클릭했을 때 실행할 메소드를 정의
// 메소드의 매개변수에 Button의 상위 클래스인 View 타입의 매개변수를 반드시 정의
fun btnClick(view:View){
Log.d("lifecycle","버튼이 눌렸습니다.") // android studio 하단에 Logcat에 로그 표시
Toast.makeText(this, "버튼이 눌렸습니다.", Toast.LENGTH_LONG).show() // 화면에 메시지 표시
}
}
activity_life_cycle_test.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LifeCycleTestActivity">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="btnClick"
android:text="@string/myok" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/mycancel" />
</LinearLayout>
[3] Layout
1. 기본 속성
1) android:id=""
- control(위젯) 구분을 위한 이름
2) 크기
① android:layout_width=""
② android:layout_height=""
- 속성값
- wrap_content : 내용 크기만큼만 자리를 차지
- match_parent : 부모의 크기만큼 자리를 차지
3) 텍스트
① android:text="텍스트"
② android:textSize="숫자sp"
③ android:textColor="#RGB값"
4) 배경
- android:background="#RGB값"
5) android:visibility=""
- visible : 보임 (default)
- invisible : 보이진 않지만 공간은 차지
- gone : 보이지도 않고 공간도 차지하지 않음
2. LinearLayout
1) android:orientation=""
- horizontal : control들이 수평으로 정렬
- vertical : control들이 수직으로 정렬
2) android:layout_weight=""
- 빈 공간을 차지하는 비율 설정
3) android:weightSum=""
- layout_weight 들의 합을 설정 (없어도 비율 설정은 가능하다.)
4) 예제1
linear_test01.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">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ff0000"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#00ff00"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#0000ff"/>
</LinearLayout>
5) 예제2
linear_test02.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#ff0000"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#00ff00"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#0000ff"/>
</LinearLayout>
- 끝 -