[Must Have Joyce의 안드로이드 앱프로그래밍] 3장 레이아웃 에디터와 레이아웃 파일

알린·2024년 1월 20일
0

레이아웃

  • 여러 UI 요소(버튼, 텍스트, 이미지 등)를 화면에 배치한 모습
  • UI 요소들을 뷰라고 함

레이아웃의 종류

  • LinearLayout
  • RelativeLayout
  • FrameLayout
  • TableLayout

MainActivity.kt에서 사용할 레이아웃 파일 지정하기

레이아웃 파일 생성 방법

  • [layout] ➡️ 마우스 우클릭 ➡️ [New] ➡️ [Layout Resourcee File]

layout 폴더가 없을 시 아래 포스팅 참고

👉 layout 폴더 만들기

레이아웃 파일 Activity.kt에서 지정 방법

  • onCreate() 함수 안에 있는 setContentView() 함수에 xml 파일 지정하기
import android.os.Bundle
import androidx.activity.ComponentActivity

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //xml 파일 지정
        setContentView(R.layout.activity_main)
    }
}

XML 작성법

<TextView android:text="Hi" />
  • < ... /> 👉 시작기호, 닫는 기호
  • TextView 👉 태그
  • android 👉 네임스페이스
  • text 👉 속성
  • 네임스페이스:속성 👉 요소
  • "Hi" 👉

전체 코드

<!-- activity_main.xml -->
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:text="Hi"
        android:textSize="35dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

💡 tip

Ctrl + Alt + L 👉 코드 자동 정렬

profile
Android 짱이 되고싶은 개발 기록 (+ ios도 조금씩,,👩🏻‍💻)

0개의 댓글