AndroidでTabHost(TabWidgetとFrameLayout)の使い方

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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!– TabHostのタグ –>
<TabWidget
android:id="@android :id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>

<!– TabHostの内容 –>
<FrameLayout
android:id="@android :id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<!– 最初のラベルの内容が表示される –>
<ImageView
android:id="@+id/homeimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@+drawable/homeimage"
android:background="#FFFFFF" ></ImageView>

<!– 第2のラベルの内容が表示される –>
<LinearLayout
android:id="@+id/time"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#000000″>
<AnalogClock
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" ></AnalogClock>
<DigitalClock
android:textColor="#FFFFFF"
android:gravity="center_horizontal"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" ></DigitalClock>
</LinearLayout>

<!– 第3のラベルの内容が表示される –>
<TextView
android:id="@+id/personlist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="联系人列表">
</TextView>

<!– 第4のラベルの内容が表示される –>
<LinearLayout
android:id="@+id/searcher"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:layout_weight="5″
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="検索キーワードを入力してください" ></EditText>
<Button
android:layout_weight="1″
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="検索"> </Button>
</LinearLayout>

</FrameLayout>
</LinearLayout>
</TabHost>

2.TabHostのよく利用メソッド
public void addTab (TabHost.TabSpec tabSpec);    //Tabを追加
public int getCurrentTab ();             //現在のtabのインデックスを取得
public String getCurrentTabTag ();          //現在のtabのtagを取得
public View getCurrentTabView ();          //現在のtabのビューを取得
public void setCurrentTab (int index);        //どのtabを表示
public void setCurrentTabByTag (String tag);    //どのtabを表示

 

3.TabHost.TabSpec
onCreateメソッド
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mTabHost = getTabHost(); //TabHostオブジェクトを取得

//TabHostにHP tabを追加
mTabHost.addTab(mTabHost.newTabSpec(“home")
.setIndicator(“ホーム", getResources().getDrawable(R.drawable.home))//Tabのラベルとアイコンを設定
.setContent(R.id.homeimage)); //Tabのコンテンツを設定

//tabHostに時刻tabを追加
mTabHost.addTab(mTabHost.newTabSpec(“time")
.setIndicator(“時刻", getResources().getDrawable(R.drawable.time))
.setContent(R.id.time));

//TabHostに連絡先tabを追加
mTabHost.addTab(mTabHost.newTabSpec(“persons")
.setIndicator(“連絡先", getResources().getDrawable(R.drawable.persons))
.setContent(R.id.personlist));

//TabHostに検索tabを追加
mTabHost.addTab(mTabHost.newTabSpec(“searcher")
.setIndicator(“検索", getResources().getDrawable(R.drawable.search))
.setContent(R.id.searcher));

mTabHost.setBackgroundResource(R.drawable.background); //TabHostの背景画像を設定
mTabHost.setCurrentTab(0); //tabの表示を設定
mTabHost.setOnTabChangedListener(this); //TabHostのイベントリスナーを設定

Android

Posted by arkgame