LayoutInflater

Inflate 함수

스크린샷 2023-01-10 오후 11.26.54.png

예시

Inflater 가져오는 법

val inflater = LayoutInflater.from(this)
val inflater2 = this.getSystemService(LAYOUT_INFLATER_SERVICE)
val inflater3 = layoutInflater //activity 에서는 이렇게도 불러 올 수 있다.

스크린샷 2023-01-11 오전 10.00.20.png

//activity1 에서 inflater

val inflater = LayoutInflater.from(this)
val inflater2 = this.getSystemService(LAYOUT_INFLATER_SERVICE)
val inflater3 = layoutInflater //activity 에서는 이렇게도 불러 올 수 있다.

println("inflater1 : ${inflater.hashCode()}")
println("inflater2 : ${inflater2.hashCode()}")
println("inflater3 : ${inflater3.hashCode()}")
/*
inflater1 : 90765789
inflater2 : 90765789
inflater3 : 90765789
* */

//activity2에서 inflater

val inflater = LayoutInflater.from(this)
val inflater2 = this.getSystemService(LAYOUT_INFLATER_SERVICE)
val inflater3 = layoutInflater //activity 에서는 이렇게도 불러 올 수 있다.

println("inflater1 : ${inflater.hashCode()}")
println("inflater2 : ${inflater2.hashCode()}")
println("inflater3 : ${inflater3.hashCode()}")
/*
inflater1 : 74495850
inflater2 : 74495850
inflater3 : 74495850
* */

사용법

//child_layout.xml - Inflate함수에서 resource 파라미터로 들어갈 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:id="@+id/fragment_root"
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:layout_gravity="center"
    android:background="#B07FB8">

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/fragment_textview"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#9C27B0"
        android:gravity="center"
        android:text="child view"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

스크린샷 2023-01-11 오전 11.26.55.png

//parent_layout.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:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/textview"
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:background="#E3AA"
        app:layout_constraintBottom_toTopOf="@id/container"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <FrameLayout 👈 이 ViewGroup에 child를 붙일 것 입니다.
        android:id="@+id/container"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#CDDC39"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textview" />

</androidx.constraintlayout.widget.ConstraintLayout>

스크린샷 2023-01-11 오전 11.34.06.png

Activity에서 사용