*Tabコントロールの使い方 [#x8ac8654]
Tabコントロールを使う場合、決まりごとがあります。~
#contents

**res/layoutに作成するXML内の決まり [#i05293a6]
レイアウトのxmlの内容は以下のような構成になります。
#codeprettify{{
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@android:id/tabhost"
    >
        <!-- Tab -->
        <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content"></TabWidget>

 <?xml version="1.0" encoding="utf-8"?>
 <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:id="@android:id/tabhost"
     >
         <!-- Tab -->
         <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content"></TabWidget>
 
         <!-- Tabの内容 -->
         <FrameLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@android:id/tabcontent">
         </FrameLayout>
 </TabHost>
        <!-- Tabの内容 -->
        <FrameLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@android:id/tabcontent">
        </FrameLayout>
</TabHost>
}}


|タグ       |説明|h
|TabHost    |android:idは必ず''@android:id/tabhost''にしなければならない。|
|TabWidget  |android:idは必ず''@android:id/tabs''にしなければならない。|
|FrameLayout|android:idは必ず''@android:id/tabcontent''にしなければならない。|

決まり事といっても、上記の表の物だけです。

*決まり事 [#f2d6aa37]
|TabWidgetのid|android:id="@android:id/tabs"|
|FrameLayoutのid|android:id="@android:id/tabcontent"|

**ActivityはTabActivityを使う [#je7bf12f]
Tabコントロールを使う場合は、TabActivityを使います。~
TabActivityには、Tabコントロールを使うためのメソッドが用意されているためです。~

***タブを作成する例 [#n3023b03]
 public class TabSampleActivity extends TabActivity{
   public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
 
     TabHost host = getTabHost();
     TabSpec spec = null;
     spec = host.newTabSpec("Tab1");
     spec.setIndicator("Tab1", getResources().getDrawable(android.R.drawable.ic_menu_view));
     spec.setContent(new Intent(this, SampleTabView.class));
     host.addTab(spec);
   }
 }
#codeprettify{{
public class TabSampleActivity extends TabActivity{
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TabHost host = getTabHost();
    TabSpec spec = null;
    spec = host.newTabSpec("Tab1");
    spec.setIndicator("Tab1", getResources().getDrawableandroid.R.drawable.ic_menu_view));
    spec.setContent(new Intent(this, SampleTabView.class));
    host.addTab(spec);
  }
}
}}
まず、''getTabHost()''でタブを管理するTabHostクラスのインスタンスを取得します。~
その後、TabHostインスタンスを使って、TabSpecインスタンスを生成します。~
TabSpecは1つのタブの内容を管理するクラスです。~
このクラスに、いろいろな設定を行い、TabHost#addTab()メソッドで、TabSpecインスタンスをTabHostに追加します。

**補足 [#fea9447a]
XMLでViewを指定する場合、includeタグで指定することも可能です。
(まぁ、結果的にViewを指定することになるんだけど)

トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS