TabActivity 구성 방법(xml 레이아웃 파일 이용)
1. xml 파일의 구성
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp" />
</LinearLayout>
</TabHost>
XML파일 작성시 요구사항
- 탭호스트: android:id="@android:id/tabhost"
- 탭 위젯: @android:io="@android:id/tabs"
- 탭컨텐트: @android:id="@android:id/tabcontent"
2. java파일 작성
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
public class HelloTabWidget extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
TabHost.TabSpec spec;
Intent intent;
super.onCreate(savedInstanceState);
//반드시 getContentView()가 getTabHost()보다 먼저 호출되어야 함
setContentView(R.layout.main);
Resources res = getResources();
TabHost tabHost = getTabHost();
// TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
// tabHost.setup();
// Artist 탭
intent = new Intent().setClass(this, ArtistsActivity.class);
spec = tabHost.newTabSpec("artists").setIndicator("Artists",
res.getDrawable(R.drawable.ic_tab_artists)).setContent(intent);
tabHost.addTab(spec);
// Album 탭
intent = new Intent().setClass(this, AlbumsActivity.class);
spec = tabHost.newTabSpec("albums").setIndicator("Albums",
res.getDrawable(R.drawable.ic_tab_albums)).setContent(intent);
tabHost.addTab(spec);
// Song 탭
intent = new Intent().setClass(this, SongsActivity.class);
spec = tabHost.newTabSpec("songs").setIndicator("Songs",
res.getDrawable(R.drawable.ic_tab_songs)).setContent(intent);
tabHost.addTab(spec);
// 앱이 실행될때 초기에 선택되어질 탭
tabHost.setCurrentTab(2);
}
}