LayoutInflater 에 대해 간단히 알아보자
Android 2015. 4. 1. 17:52http://developer.android.com/reference/android/view/LayoutInflater.html
레이아웃 XML을 View 오브젝트로 변경해주는 추상클래스로
getLayoutInflater() 나 getSystemService(String) 을 사용하여 LayoutInflater 객체를 얻어서 사용한다.
1 2 | LayoutInflater inflater = (LayoutInflater)context.getSystemService (Context.LAYOUT_INFLATER_SERVICE); | cs |
간단하게 XML 로 만든 레이아웃을 View 오브젝트로 바꿔보면.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:sl="http://schemas.android.com/apk/res/com.genikids.genischoolhistory" android:layout_width="match_parent" android:layout_height="match_parent" > <com.genikids.genischoolhistory.ui.ScalableLayout android:layout_width="match_parent" android:layout_height="match_parent" sl:scale_base_height="184" sl:scale_base_width="167" > <ImageView android:id="@+id/imgThumbnail" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/gs_age_b1" sl:scale_height="184" sl:scale_left="0" sl:scale_top="0" sl:scale_width="167" /> <ImageView android:id="@+id/imgDownOrCheck" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/gs_check01" android:visibility="visible" sl:scale_height="33" sl:scale_left="134" sl:scale_top="0" sl:scale_width="33" /> </com.genikids.genischoolhistory.ui.ScalableLayout> </RelativeLayout> | cs |
두 개의 ImageView 를 가진 CustomLayout을 만들고,
1 2 3 4 5 6 7 8 9 10 | // CustomLayout View 객체가 추가될 부모 뷰 LinearLayout li = (LinearLayout)findViewById(R.id.ageBg); // CustomLayout을 View로 변신시키기. View v = getLayoutInflater().inflate(R.layout.item_age, null); // 제어를 위해 ID를 부여하고 v.setId(i + AGE1); // 부모뷰에 추가 li.addView(v); // 리스너를 추가 v.setOnClickListener(imageClickListener); | cs |
요렇게 CustomLayout으로 된 View 오브젝트로 만들어 제어할 수 있다.