메뉴 건너뛰기

조회 수 266 추천 수 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
번호 제목 날짜 조회 수
217 안드로이드 - 커스텀 폰트(Custom Font) 적용하기 file 2021.04.02 344
216 Volley 로 웹요청하고 응답받기1 - Get방식 file 2020.12.14 348
215 Volley 이용시에 한글 깨질때 UTF-8로 변경 2020.12.14 348
214 안드로이드 - 인텐트(Intent)를 활용한 액티비티(Activity)간 데이터 전달하기 file 2021.03.31 349
213 Fragment에서 Toast 사용하기 2020.12.14 376
212 Volley 로 웹요청하고 응답받기3 - Get방식 , json 읽기 (php,mysql) file 2020.12.14 378
211 안드로이드 - 리사이클러 뷰(RecyclerView) 구현 file 2021.04.01 390
210 안드로이드 - SharedPreferences에 앱 정보 저장하기 file 2021.04.02 395
209 안드로이드 스튜디오 - 코드 자동 들여쓰기 file 2021.03.29 420
208 안드로이드 - 프로그레스바(ProgressBar) 구현하기 file 2021.04.01 451
207 안드로이드 - 툴바(ToolBar)를 사용하여 앱바(App Bar) 구현하기 file 2021.04.01 455
206 안드로이드 입문 연습문제 3문항 - CheckBox, RadioButton, EditText, Spinner, 이벤트연습 file 2020.12.14 480
205 구글맵 snippet을 두줄이상으로 구현하기 file 2020.12.14 481
204 안드로이드 - 액티비티(Activity)로부터 결과 데이터 받아오기 file 2021.03.31 483
203 안드로이드 - 프래그먼트 (Fragment) 사용하기 file 2021.04.02 486
202 안드로이드 - 리스트뷰(ListView) 구현 file 2021.04.01 490
201 안드로이드 - RecyclerView 안에 RecyclerView 구현하기 file 2021.04.02 502
200 안드로이드 - 프레임레이아웃 (FrameLayout) file 2021.03.29 517
199 안드로이드 - 색상 리소스 (Color Resource) 추가 </color> file 2021.03.31 551
198 안드로이드 - Text 입력 이벤트 처리 - TextWatcher file 2021.04.02 557
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 13 Next
/ 13

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved