메뉴 건너뛰기

조회 수 227 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

1. setContentView() 함수의 역할

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:id="@+id/layout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:context="com.example.myapplication.MainActivity"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="81dp">

    <LinearLayout
        android:id="@+id/layout"
        android:layout_width="368dp"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="8dp">

        <TextView
            android:id="@+id/textView3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Hello World!!" />
    </LinearLayout>

</android.support.constraint.ConstraintLayout>

▼ 여기 화면 구성을 위한 XML 레이아웃 리소스가 있습니다. 해당 XML은 화면에 배치되는 View와 ViewGroup에 대한 속성과 상하 배치관계 등과 같이 화면에 배치되기 위한 여러 정보가 담겨있습니다. 해당 XML 파일은 단순한 디자인 정보로 실제로 이 정보를 가지고 화면을 보여주기 위해서는 XML에 정의된 각 위젯들을 정의된 속성을 지정하고 상하관계에 맞춘 뒤 메모리에 올려야 합니다.

이러한 일련의 작업을 소스상에서 제공하는 게 setContentView() 함수입니다.

public class MainActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

▼ 프로젝트를 생성하면 액티비티를 상속받는 클래스가 default로 제공됩니다. 개발자 편의를 위해 자동으로 지원되는 기능으로 Activity 클래스를 상속받으면 반드시 onCreate() 함수를 오버 라이딩합니다. 해당 함수는 액티비티(Activity)가 실행될 때 가장 먼저 실행되는 함수로 마치 자바에서 프로그램 시 가장 먼저 실행되는 main() 함수와 비슷합니다. 

 

▼ setContentView() 함수는 첫 번째 인자로 넘겨주는 XML 레이아웃 리소스 ID에 해당하는 파일을 파싱 하여 뷰(View)를 생성하고 뷰(View)의 속성을 지정하고 뷰(View) 간의 상하관계에 맞춰 배치를 합니다. 이러한 일련의 과정을 전개(Inflate)라 부릅니다. 

setContentView() 함수는 xml 문서를 전개하기 위해 내부적으로 LayoutInflater 클래스를 참조합니다. 


2. 전개자 (Inflater)

XML 문서를 전개(Inflate) 하기 위해서 시스템상으로 제공하는 클래스가 있습니다. 바로 LayoutInflater 클래스로 해당 클래스의 객체를 구하는 방법은 아래 두 가지가 있습니다. 

        LayoutInflater inflater1 = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LayoutInflater inflater2 = getLayoutInflater();

첫 번째 방법은 getSystemService() 함수를 통해 가져오는 방법입니다. 두 번째는 액티비티 내에서 제공하는 getLayoutInflater() 함수를 통해 받아오는 방법이 있습니다. 

View view  = getLayoutInflater().inflate(R.layout.activity_sub, null);

전개자(Inflater)를 생성하였다면 inflate() 함수를 통해 전개된 뷰(View) 객체를 반환받습니다. 첫 번째 인자로는 XML 레이아웃 리소스 ID를 넘기고 두 번째는 전개할 레이아웃의 최상위 Root ViewGroup으로 설정할 객체를 넘깁니다. 아래는 Inflater를 통해 전개된 뷰(View)를 Dialog 창에 Setting 하여 팝업을 띄우는 예제입니다.

public class MainActivity extends AppCompatActivity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        View view  = getLayoutInflater().inflate(R.layout.activity_sub, null);

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(view);

        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }
}

 


List of Articles
번호 제목 날짜 조회 수
237 안드로이드 - RecyclerView 안에 RecyclerView 구현하기 file 2021.04.02 502
236 안드로이드 - Json 파싱하기 file 2021.04.02 828
235 안드로이드 - 타이머(Timer) 구현하기 2021.04.01 1368
234 안드로이드 - SQLiteDatabase 구현하기 file 2021.04.01 241
233 안드로이드 - 리사이클러 뷰(RecyclerView) 구현 file 2021.04.01 388
232 안드로이드 - 네비게이션 드로어(Navigation Drawer)를 활용하여 슬라이드 메뉴 구현하기 file 2021.04.01 1855
231 안드로이드 - 툴바(ToolBar)를 사용하여 앱바(App Bar) 구현하기 file 2021.04.01 448
230 안드로이드 - 프로그레스바(ProgressBar) 구현하기 file 2021.04.01 451
229 안드로이드 - AsyncTask 구현 예제 file 2021.04.01 280
228 안드로이드 - 액티비티(Activity)와 액티비티 생명 주기(Activity Life Cycle) file 2021.04.01 225
227 안드로이드 - 리스트뷰(ListView) 구현 file 2021.04.01 490
226 안드로이드 - 컨텍스트 메뉴(Context Menu) 사용 예제 file 2021.04.01 206
225 안드로이드 - 옵션 메뉴 (Option Menu) 구현 방법 file 2021.04.01 283
224 안드로이드 - 명시적 인텐트(Explicit Intent)와 암시적 인텐트 (Implicit Intent) file 2021.04.01 324
223 안드로이드 - 날짜 및 시간 정보 입력받기 (DatePickerDialog / TimePickerDialog) file 2021.04.01 1759
» 안드로이드 - setContentView()와 레이아웃 전개자(LayoutInflater) 2021.04.01 227
221 안드로이드 - AlertDialog 사용하여 알림창 띄우는 방법 총정리 file 2021.03.31 685
220 안드로이드 - SnackBar를 사용해 팝업창 띄우기 file 2021.03.31 279
219 안드로이드 - 토스트(Toast) 메시지 사용하기. file 2021.03.31 321
218 안드로이드 - 액티비티(Activity)로부터 결과 데이터 받아오기 file 2021.03.31 483
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 13 Next
/ 13

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved