2013. 7. 15. 16:37

안드로이드(Android) 에서 Tab을 구현하실때 여러가지 방법이 있는데요 그중에 Activity 를 통한 구현 방법을 소개해 드리겠습니다.


먼저 MainActivity 에서 원래 exdends Activity 로  Activity 를 상속받지만 Tab 을 구현하기위해서는 extends TabActivity 를 상속 받습니다.  그리고 mian xml 에서는 다음과 같이 코드를 짜줍니다.


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

     xmlns:tools="http://schemas.android.com/tools"

     android:id="@+id/LinearLayout1"

     android:layout_width="fill_parent"

     android:layout_height="fill_parent"

     android:orientation="vertical" >


   <TabHost

        android:id="@android:id/tabhost"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent" >


          <LinearLayout

              android:layout_width="fill_parent"

              android:layout_height="fill_parent"

              android:orientation="vertical" >


             <TabWidget

                 android:id="@android:id/tabs"

                 android:layout_width="wrap_content"

                 android:layout_height="wrap_content"

             </TabWidget>


           <FrameLayout

               android:id="@androidid:id/tabcontent"

               android:layout_width="fill_parent"

               android:layout_height="fill_parent" >

           </FrameLayout>


        </LinearLayout>


     </TabHost>


</LinearLayout>


이렇게 xml 코드를 다 작성해주시고 이제 onCreate 메소드 안에 이렇게 코딩을 하셔서 사용하시면됩니다.


TabHost tabhost = getTabHost();


TabSpec firsttabspec = tabhost.newTabSpec("tag1");

firsttabspec.setIndicator("첫번째");

Intent firstintent = new Intent(this, FirstActivity.class); //FirstActivity 클래스는 그냥 Activity 상속받아 만들어주면됨니다.

Firsttabspec.setContent(firstintent);


TabSpec scendtabspec = tabhost.newTabSpec("tag2");

scendtabspec.setIndicator("두번쨰");

Intent scendintent = new Intent(this, ScendActivity.class); //ScendActivity 도 마찬가지구요

scendtabspec.setContent(scendintent);


tabhost.addTab(firsttabspec);

tabhost.addTab(scendtabspec);


이렇게 적어주시면 간단하게 Tab 이 구현됩니다. 그리고 각 Tab 에 내용들은 각 만들어준 FirstActivity, ScendActivity 클래스에서

보통 텍스트나 에디트등을 set 하셔도되고 get 하셔도되고 구현 하시면됩니다. 각 만들어준 두 엑티비티클래스들도 각각의 xml을 

가지는것은 당연한거겟죠?  xml 짜시는것도 main xml 짜듯이 드래그앤드랍하셔서 작성하시면 됩니다. 불꽃코딩 !!


Posted by 광스