[android]06. 인플레이션

목차

  1. 인플레이션
  2. LayoutInflater
  3. LayoutInflater 예시

인플레이션

프로젝트를 만들면 첫 화면을 위한 XML 레이아웃과 소스 파일이 자동으로 만들어집니다. 이 자바 소스 코드에서 XML 레이아웃 파일을 이해하려면 setContentView 메소드의 파라미터로 해당 XML 레이아웃 파일을 지정해주어야 합니다. R.layout.activity_main 을 전달해주어야 하는 거죠. 그렇게 하면 내부적으로 인플레이션 과정이 진행됩니다. XML 레이아웃 파일 안에 들어있는 뷰 태그들을 이용해 뷰 객체를 메모리에 만드는 과정입니다. 디자인할 때 버튼을 추가한다면 xml 코드에서는 new Button과 같이 실제 코드로 생성하지 않습니다. setContentView가 실제로 메모리에 Button 객체를 생성합니다.



LayoutInflater

(activity_main 안에 포함되지 않는) 부분 화면인 경우에는 직접 메모리에 객체화해야 합니다. 아래 예시를 통해 살펴봅시다.


LayoutInflater 예시

sub1.xml 부분화면을 만들어 봅시다. getSystemService 메서드를 통해 인플레이터 객체를 생성할 수 있습니다. 뷰 객체(여기서는 FrameLayout, container)에 인플레이션한 결과물(sub1)을 설정할 수 있습니다.

java접기/펼치기
public class MainActivity extends AppCompatActivity {
    FrameLayout container;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        container = (FrameLayout) findViewById(R.id.container);

        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                inflater.inflate(R.layout.sub1, container, true);
            }
        });
    }
}
sub1.xml 접기/펼치기
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorAccent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="부분화면"
        android:textSize="40dp" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
</LinearLayout>
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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.049"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.023" />

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="408dp"
        android:layout_height="508dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button"
        app:layout_constraintVertical_bias="0.427">

    </FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

0601