Android - Inflater
android inflater
사용자 UI, 즉 View를 만들기위해서는 XML나 Code로 구현해 주어야 한다.
[XML]
<Button
android:id="@+id/btn_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
혹은
[Code]
Button myButton = new Button(this);
RelativeLayout myLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams buttonParams =
new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
myLayout.addView(myButton, buttonParams);
setContentView(myLayout);
Code를 보게 되면 Button 을 표시하기 위해 Button instance를 생성하고, setContentView를 통해 화면에 보여주게 된다.
반면 XML의 경우는 화면에 보여주기 위해 다음과 같은 변환 과정을 거쳐야 한다.
XML -(inflate)-> Code -> 화면 표시
LayoutInflater inflater = (LayoutInflater) getSystemService( Context.LAYOUT_INFLATER_SERVICE ); LinearLayout linearLayout = (LinearLayout) inflater.inflate( R.layout.inflate_test, null ); setContentView( linearLayout );
여기서 XML로 사용된 inflate_test layout의 root element를 확인해 해당 type으로 inflate해주어야 한다.