메뉴 건너뛰기

조회 수 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
번호 제목 날짜 조회 수
257 Activity Data Transfor/ 액티비티 이동간에 데이터 전송하기 file 2016.06.07 7676
256 Activity Switching / 안드로이드 액티비티 전환 / 화면 전환 file 2016.06.07 8311
255 Android Login and Registration with PHP, MySQL and SQLite file 2015.07.16 14178
254 Android Navigation Drawer API 공개! 디자인 가이드 살펴보기 file 2015.07.29 8141
253 Android Push GCM 서버 구성 하기(3) file 2015.12.14 6388
252 Android Push GCM 프로젝트 앱 적용 하기(2) file 2016.03.18 8956
251 android SMS 리시버 2015.06.29 6871
250 Android Studio에서 SQLCipher 라이브러리 추가 방법 file 2018.10.02 1776
249 Android TIP] strings.xml 에서 특수문자 사용하기 2015.12.15 6629
248 Android 간단한 회원 가입 폼 만들기 for Mac (PHPMyAdmin 이용) file 2015.07.10 10511
247 Android 와 JSP 간 파라미터 암복호화 (1) file 2016.05.26 7474
246 Android 와 JSP 간 파라미터 암복호화 (2) 2016.05.26 7741
245 Android 와 JSP 간 파라미터 암복호화 (3) file 2016.05.26 8091
244 android.support.v4.content.FileProvider not found file 2020.12.14 308
243 AndroidManifest에 선언한 메타데이터(meta-data) 가져오기 2016.06.10 9322
242 Android] Fragment 내부의adapter에서 startActivity 하기 2015.12.15 6487
241 Android] 안드로이드 홈 디렉토리 알아내기 2015.12.15 6895
240 Apk manager 이용해 Decompile (디컴파일) 하기 file 2021.03.16 1624
239 App 실행 file 2021.03.31 244
238 CSS3 Rounded Corner, 그림자 효과 사용하기 file 2014.09.04 6762
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 13 Next
/ 13

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved