2012년 1월 7일 토요일

Implementing Dynamic Tab Widget

1. layout xml file (main.xml) 만들기

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost" 
    android:layout_height="fill_parent"
    android:layout_width="fill_parent">
     <LinearLayout
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:orientation="vertical"
         android:padding="5dp">

         <TabWidget
             android:id="@android:id/tabs"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"/>
          <FrameLayout
             android:id="@android:id/tabcontent"             android:layout_height="wrap_content"
             android:layout_width="fill_parent"
             android:padding="5dp"/>
    </LinearLayout>
</TabHost>
- xml의 계층을 건들지 마시고 각 id의 name은 미리 android에 선언된 이름이므로 바꾸지 말아야 한다.

2. MainActivity 만들기
    - Tab을 만들기 위해서는 우선 TabActivity를 상속받은 Activity를 만들고,  TabHost.TabContentFactory 를 implement해야 한다.

public class TabTestActivity extends TabActivity implements TabHost.TabContentFactory {
          
             .......
       @Override
       public void onCreate(Bundle savedInstanceState) {

           super.onCreate(savedInstanceState);
           // 위에서 만든 main.xml           setContentView(R.layout.main);
           // main.xml 내에 있는 @android:id/tabhost 
           TabHost tabHost = getTabHost();    
       
           // 1. 새로운 tab생성. 물론 생성되는 모든 TabSpec들은 @android:id/tabs 내에 포함되게 됨
           TabHost.TabSpec tabSpec = tabHost.newTabSpec(getString(R.string.str_appranker_tab1));
           // 2. 이부분에서 단순히 String만을 주게 되면 Tab Widget에 String 만 표시된다.

           // 만약, 더 다양한 효과를 주고 싶으면 이부분에 View를 넘기면 된다.
           tabSpec.setIndicator(getString(R.string.str_appranker_tab1));
           // 3. 이부분이 상단 Tab을 눌렀을때 하단에 표시되는 내용부분이다.
           // 직접, View를 넘길수 없으며 Dynamic하게 생성되는 View들을 FrameLayout내에 넣어야 하기에 TabContentFactory를 이용하게 된다.
            tabSpec.setContent(this);
            // 4. TabHost에 생성한 Tab을 넣는다.
            // Tab을 더 생성하고 싶으면 1~4 다시 수행.            tabHost.addTab(tabSpec);
       }

       @Override
       public View createTabContent(String tag) {

           LayoutInflater inflater = getLayoutInflater();
           // Tab을 구별할 수 있는 tag 값에 따라 다른 View를 R.layout.content inflate해서 넣으면 되며, 가장 중요한 것은 @android:id/tabcontent의 하위 view로 rootview를 정의해야한다는것..
           // 그래야 나중에 framelayout에서 보이고 안보이고를 결정해 주죠......
           if(tag.equals(getString(R.string.str_tab1))){
                     return inflater.inflate(R.layout.content, getTabHost().getTabContentView()); 
           }
           return null;
       }
}


-- 근데요.. 하나의 activity내에서 FrameLayout에서 보여주게될 복잡한 view들을 control한다는것은 조금 힘들고 지저분해 보이네요...
-- tabSpec.setContent()에 Intent를 넣어 Activity가 실행되게하여 MVC모델에 충실하게 만드는 것은 어떨런지.....

댓글 없음:

댓글 쓰기