TapHost : 탭호스트 화면전환

2019. 3. 19. 15:23Andriod


#1 *.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_width="match_parent"
android:layout_height="match_parent">

<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:background="#f00000"
android:orientation="horizontal"></LinearLayout>

<LinearLayout
android:id="@+id/tabArtist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f0f000"
android:orientation="horizontal">
</LinearLayout>

<LinearLayout
android:id="@+id/tabAlbum"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f00fff"
android:orientation="horizontal">
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>


#2 *.java

package com.example.a0319_02;


import android.app.TabActivity;
import android.os.Bundle;
import android.widget.TabHost;

@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity{ // 상속이 TabActivity이다. 주의할 것!

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

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);

tabHost.setCurrentTab(0);

}
}