とりあえずタブ構成の画面を作成してみる

上記のような一般的なタブ構成の画面を構築するには、

まず追加したい画面の res/layout/****.xml へ下記の青字部分のように追記する。

<?xml version="1.0" encoding="utf-8"?>

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

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

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

android:layout_width="fill_parent"

android:layout_height="fill_parent">

<LinearLayout android:layout_width="fill_parent"

android:id="@+id/linearLayout1"

android:layout_height="fill_parent"

android:orientation="vertical">

<TabWidget android:layout_width="fill_parent"

android:layout_height="wrap_content"

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

</TabWidget>

<FrameLayout android:layout_width="fill_parent"

android:layout_height="fill_parent"

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

<LinearLayout android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:id="@+id/contentlayout1">

(タブ1選択時の内容はここへTextViewなどで準備する)

</LinearLayout>

<LinearLayout android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:id="@+id/contentlayout2">

(タブ2選択時の内容はここへTextViewなどで準備する)

</LinearLayout>

<LinearLayout android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:id="@+id/contentlayout3">

(タブ3選択時の内容はここへTextViewなどで準備する)

</LinearLayout>

(タブの必要な数分、LinearLayout をココへ用意する)

</FrameLayout>

</LinearLayout>

</TabHost>

</LinearLayout>

次に、Activity の onCreate などで下記のように処理を書けば出来ます。

// TabHostクラスを取得

TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);

// TabHostクラス初期化

tabHost.setup();

// タブ1 設定

TabSpec tab1 = tabHost.newTabSpec("tab1"); // タブの生成

tab1.setIndicator("タブ1"); // タブに表示する文字列をセット

tab1.setContent(R.id.contentlayout1); // タブ選択時に表示するビューをセット

tabHost.addTab(tab1); // タブホストにタブを追加

// タブ2 設定

TabSpec tab2 = tabHost.newTabSpec("tab2"); // タブの生成

tab2.setIndicator("タブ2"); // タブに表示する文字列をセット

tab2.setContent(R.id.contentlayout2); // タブ選択時に表示するビューをセット

tabHost.addTab(tab2); // タブホストにタブを追加

// タブ3 設定

TabSpec tab3 = tabHost.newTabSpec("tab3"); // タブの生成

tab3.setIndicator("タブ3"); // タブに表示する文字列をセット

tab3.setContent(R.id.contentlayout3); // タブ選択時に表示するビューをセット

tabHost.addTab(tab3); // タブホストにタブを追加

// 初期表示設定

tabHost.setCurrentTab(0);