2swan

Android 탭 호스트 예제 본문

Programming/Android Example

Android 탭 호스트 예제

2swan 2023. 8. 3. 00:11

tab1.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity7_tab"
    android:id="@android:id/tabhost">

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </TabWidget>


    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:id="@+id/tabSong"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:background="#8FA3B3">
        </LinearLayout>
    <LinearLayout
        android:id="@+id/tabArtist"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="#BA7B7B">
    </LinearLayout>
    <LinearLayout
        android:id="@+id/tabAlbum"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="#A5872C">
    </LinearLayout>
    </FrameLayout>
    </LinearLayout>
</TabHost>

 

tab1.java

public class MainActivity7_tab extends TabActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_activity7_tab);

        TabHost tabHost = getTabHost();

        TabHost.TabSpec tabSpecSong = tabHost.newTabSpec("SONG").setIndicator("음악별");
        tabSpecSong.setContent(R.id.tabSong);
        tabHost.addTab(tabSpecSong);

        TabHost.TabSpec tabSpecArtist = tabHost.newTabSpec("ARTIST").setIndicator("가수별");
        tabSpecArtist.setContent(R.id.tabArtist);
        tabHost.addTab(tabSpecArtist);

        TabHost.TabSpec tabSpecAlbum = tabHost.newTabSpec("ALBUM").setIndicator("앨범별");
        tabSpecAlbum.setContent(R.id.tabAlbum);
        tabHost.addTab(tabSpecAlbum);

    }
}

결과 값

 

 


 

tab2.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity_tab2"
    android:id="@android:id/tabhost">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <ImageView
            android:id="@+id/DOG"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/dog"/>

        <ImageView
            android:id="@+id/CAT"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/cat"/>

        <ImageView
            android:id="@+id/RAB"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:src="@drawable/rabbit"/>

        <ImageView
            android:id="@+id/HORSE"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:src="@drawable/horse"/>

    </FrameLayout>
    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#00BCD4">
    </TabWidget>

</LinearLayout>

</TabHost>

 

tab2.java

public class MainActivity_tab2 extends TabActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_tab2);

        TabHost tabHost = getTabHost();

        TabHost.TabSpec tabSpecDog= tabHost.newTabSpec("Dog").setIndicator("강아지");
        tabSpecDog.setContent(R.id.DOG);
        tabHost.addTab(tabSpecDog);

        TabHost.TabSpec tabSpecCAT= tabHost.newTabSpec("CAT").setIndicator("고양이");
        tabSpecCAT.setContent(R.id.CAT);
        tabHost.addTab(tabSpecCAT);

        TabHost.TabSpec tabSpecRAB= tabHost.newTabSpec("RAB").setIndicator("토끼");
        tabSpecRAB.setContent(R.id.RAB);
        tabHost.addTab(tabSpecRAB);

        TabHost.TabSpec tabSpecHORSE= tabHost.newTabSpec("HORSE").setIndicator("말");
        tabSpecHORSE.setContent(R.id.HORSE);
        tabHost.addTab(tabSpecHORSE);

    }
}

결과 값