LayoutInflater
- xml에 정의해놓은 레이아웃을 View 객체로 인스턴스화 하는 클래스
- xml에 정의해놓은 레이아웃을 View 객체로 인스턴스화 하여 메모리에 올려주는 클래스
- xml 레이아웃을 View 객체로 인스턴스화 하는 함수
- resource : 뷰 객체로 만들고 싶은 xml 레이아웃 id
- root :
- attachToRoot==true면, root으로 들어오는 viewGroup이 새로 만들어지는 뷰(resource)의 부모 뷰가 됩니다.
- attachToRoot==false면, root으로 들어오는 ViewGroup은 새로만들어지는 뷰(resource) root의 LayoutParams값을 적용하기 위한 객체일뿐 새로만들어지는 뷰가 ViewGroup의 자식 뷰가 되는건 아닙니다.
- root은 null이 될 수 있습니다.
- attachToRoot : 새로만들어진 뷰가 root parameter으로 들어가는 ViewGroup에 자식으로 attach 되어야 하는지 여부
- attachToRoot==true면, 새로만들어진 뷰가 root parameter으로 들어가는 ViewGroup에 자식으로 attach 됩니다.
- attachToRoot==false면, root으로 들어오는 ViewGroup은 새로만들어지는 뷰(resource) root의 LayoutParams값을 적용하기 위한 객체일뿐 새로만들어지는 뷰가 ViewGroup의 자식 뷰가 되는건 아닙니다.
- returns : View객체를 리턴
- root parameter가 null이 아니고, attachToRoot가 true 라면 root parameter 뷰의 root 입니다. resource는 root viewGroup의 자식으로 attach 됩니다.
- 그렇지 않다면, 새로만들어진 뷰(resource)의 root 입니다.
- root parameter이 null 이라면, (attachToRoot boolean 여부에 상관없이) attach 될 부모 뷰가 없기 때문에 새로 만들어진 뷰(resource)의 root이 리턴 될 수 밖에 없습니다.
- root paramater이 null이 아니고, attachToRoot이 false라면,새로만들어지는 뷰(resource)가 ViewGroup의 자식 뷰가 되는것이 아니기 때문에, 새로 만들어진 뷰(resource)의 root이 리턴 될 수 밖에 없습니다.
예시
Inflater 가져오는 법
val inflater = LayoutInflater.from(this)
val inflater2 = this.getSystemService(LAYOUT_INFLATER_SERVICE)
val inflater3 = layoutInflater //activity 에서는 이렇게도 불러 올 수 있다.
- 같은 context에서는 같은 inflater가 리턴된다.
//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>
//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>
Activity에서 사용