본문 바로가기

Language/Android

공통 레이아웃(Layout) Include 처럼 사용 하기

layout을 웹의 include 처럼 공통되는 것을 하나로 만들어서 사용할수 없나 하고... 찾던중에 LayoutInflater을 찾았다.

사용방법은


먼저 공통되는 레이아웃을 하나 만든다.
res/layout/bottom_layout.xml
 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="
http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    /> 
</LinearLayout>

그리고 Body가 되는 부분의 layout을 만든다.
res/layout/main.xml
 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="
http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
<com.tistory.eraserdve.Bottom_layout 
// 콘트롤할 class 경로와 class 명을 입력
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:textSize="12sp"
 />
</LinearLayout>

Body를 콘트롤할 class 를 생성한다.
src/com.tistory.eraserdev/LayoutInflaterBody.java

 package com.tistory.eraserdev;

import android.app.Activity;
import android.os.Bundle;

public class LayoutInflaterBody extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}


마지막으로 공통되는 레이아웃을 콘트롤할 class 파일을 작성한다.
src/com.tistory.eraserdev/Bottom_layout.java

 package com.tistory.eraserdev;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

public class Bottom_layout extends LinearLayout{

 public Bottom_layout(Context context) {
  // TODO Auto-generated constructor stub
  super(context);
  init();

 }
 
 public Bottom_layout(Context context, AttributeSet attrs) {
  // TODO Auto-generated constructor stub
  super(context, attrs);
  init();
 
 }

 private void init() {
  // TODO Auto-generated method stub
  LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(
    Context.LAYOUT_INFLATER_SERVICE);
  inflater.inflate(R.layout.bottom_layout, this, true);
  
 }
}


이렇게 LayoutInflater로 생성되는 layout은 Activity가 아니기에 매니페스트 파일에 Bottom_layout Activity를 선언할 필요가 없다.

'Language > Android' 카테고리의 다른 글

AppWidget에서 ProgressBar 띄우기...  (1) 2010.07.01
키보드 이벤트  (0) 2010.06.28
알람 AlramManager  (0) 2010.06.21
Activity 실행되면서 softkeyBoard 띄우기  (0) 2010.06.18
putExtra int값 넘기고 받기  (1) 2010.06.18