Android : activity_main.xml 구성

Beautify.log·2022년 1월 30일
0

Android with Kotlin

목록 보기
1/17
post-thumbnail

/app/src/main/res/layout/activity_main.xml은 View를 구성하는 요소들이다.

View, Activity, Fragment란?

구분의미
View화면 내 기본적인 UI 요소, 모든 UI 요소는 View 클래스의 서브클래스
Widget과 Layout으로 구분하며 Widget은 Button, TextView, CheckBox와 같은 시각적인 UI 요소를 의미함.
Layout은 화면에서 보이지 않으나 다른 View들을 담는 container 역할을 하는 것을 의미함.
Activity하나의 화면과 밀접하게 연관되어 있는 독립형 실행 모듈
Fragment두 개 이상의 Activity를 하나의 화면에서 구성하는 것.

activity_main.xml 구성

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"
>
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="say Hello to"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" 
			android:id="@+id/sayHello" 
            tools:ignore="HardcodedText"
    />
  
    <Button
            android:text="@string/butter"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:id="@+id/butter"
            tools:ignore="MissingConstraints"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginBottom="258dp"
            android:layout_marginTop="50dp"
            app:layout_constraintTop_toBottomOf="@+id/sayHello"
            app:layout_constraintStart_toStartOf="parent"
            android:layout_marginStart="160dp"
            android:layout_marginEnd="160dp"
            app:layout_constraintEnd_toEndOf="parent"
    />


</androidx.constraintlayout.widget.ConstraintLayout>

TextView는 텍스트 상자를 나타내고, Button은 버튼을 나타냄.

MainActivity.kt

package com.test.androidstudy

import android.content.ContentValues
import android.content.DialogInterface
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AlertDialog

class MainActivity : AppCompatActivity() {

    // val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }

    override fun onCreate(savedInstanceState: Bundle?) {

        /*  biding을 사용한다면
            setContentView(R.layout.activity_main)
            setContentView(binding.root)

            binding.btnSay.setOnClickListener {
                Log.i(ContentValues.TAG, "btnSay Tapped")
                binding.sayHello.text = "Hello World!"
            }
        */
        super.onCreate(savedInstanceState)

        // layout에 있는 activity_main.xml로 화면 정의
        setContentView(R.layout.activity_main)

        val sayHello:TextView = findViewById(R.id.sayHello)
        val butter = findViewById<Button>(R.id.butter)

        // 이벤트 추가
        butter.setOnClickListener {
            sayHello.text = "What a nice Android World!"

            // Toast 만들기
            val toast = Toast.makeText(this.applicationContext, "button tapped", Toast.LENGTH_SHORT)
            toast.show()    // 토스트 보여주기

            // Alert 보여주기
            AlertDialog.Builder(this@MainActivity)
                .setTitle("Welcome to the Android World!")
                .setMessage("안드로이드 세계에 오신것을 환영합니다!!")
                .setCancelable(false)
                .setNeutralButton("닫기", DialogInterface.OnClickListener {
                    dialog, which -> // 닫기버튼을 눌렀을 때 처리할 로직을 넣어줍니다.
                }).show()
        }

    }

    override fun onStart() {
        super.onStart()
        Log.i(ContentValues.TAG, "onStart 실행")
    }

    override fun onResume() {
        super.onResume()
        Log.i(ContentValues.TAG, "onResume 실행")
    }

    override fun onRestart() {
        super.onRestart()
        Log.i(ContentValues.TAG, "onRestart 실행")
    }

    override fun onPause() {
        super.onPause()
        Log.i(ContentValues.TAG, "onPause 실행")
    }

    override fun onStop() {
        super.onStop()
        Log.i(ContentValues.TAG, "onStop 실행")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.i(ContentValues.TAG, "onDestroy 실행")
    }
}
profile
tried ? drinkCoffee : keepGoing;

0개의 댓글