標籤:android blog http java 使用 os io 2014
TabHost是一種非常實用的組件,TabHost可以很方便地在視窗上放置多個標籤頁,每個標籤頁獲得了一個與外部容器相同大小的組件擺放地區。在手機系統的應用類似“未接電話”、“已接電話”、“呼出電話”等。
1 、 TabHost提供了兩個方法來建立選項卡、添加選項卡
newTabSpec(String tag) : 建立選項卡
addTab(TabHost.TabSpec tabSpec) : 添加選項卡
2、TabHost 切換選項卡觸發的監聽是TabHost.OnTabChangeListener
下面通過案例來熟悉TabHost
使用TabHost的一般步驟為:
(1)在介面布局中為TabHost定義改選項卡的內容
(2)Activity繼承TabActivity
(3)調用TabActivity的getTabHost()方法擷取TabHost對象
(4)通過TabHost對象的方法來建立選項卡、添加選項卡
跟著上面步驟來實現TabHost案例
(1)在介面布局中為TabHost定義改選項卡的內容,一般採用FrameLayout作為根布局,每個標籤頁面對應一個子節點的Layout
<!-- 這是根布局 --> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <!-- 這是第一個Tab布局 --> <LinearLayout android:id="@+id/widget_layout_Blue" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <EditText android:id="@+id/widget34" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="EditText" android:textSize="18sp"> </EditText> <Button android:id="@+id/widget30" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button"> </Button> </LinearLayout> <!-- 這是第二個Tab布局 --> <LinearLayout android:id="@+id/widget_layout_red" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <AnalogClock android:id="@+id/widget36" android:layout_width="wrap_content" android:layout_height="wrap_content"> </AnalogClock> </LinearLayout> <!-- 這是第三個Tab布局 --> <LinearLayout android:id="@+id/widget_layout_green" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <RadioGroup android:id="@+id/widget43" android:layout_width="166px" android:layout_height="98px" android:orientation="vertical"> <RadioButton android:id="@+id/widget44" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="男"> </RadioButton> <RadioButton android:id="@+id/widget45" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女"> </RadioButton> </RadioGroup> </LinearLayout> </FrameLayout>
(2)Activity繼承TabActivity
(3)調用TabActivity的getTabHost()方法擷取TabHost對象
(4)通過TabHost對象的方法來建立選項卡、添加選項卡
package com.example.tabhost;import android.app.TabActivity;import android.os.Bundle;import android.util.Log;import android.view.LayoutInflater;import android.widget.TabHost;import android.widget.TabHost.OnTabChangeListener;@SuppressWarnings("deprecation")public class MainActivity extends TabActivity{@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);//擷取TabHost 對象TabHost tabHost = getTabHost();//設定使用TabHost布局//from(this)從這個TabActivity擷取LayoutInflater//R.layout.main 存放Tab布局//通過TabHost獲得存放Tab標籤頁內容的FrameLayout//是否將inflate 拴繫到根布局元素上LayoutInflater.from(this).inflate(R.layout.activity_main,tabHost.getTabContentView(), true);//添加第一個標籤頁//setIndicator 添加表體,可以是view//添加tab內容 布局tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("TAB1").setContent(R.id.widget_layout_Blue)); //添加第二個標籤頁tabHost.addTab(tabHost.newTabSpec("tab2")//在標籤標題上放置表徵圖.setIndicator("TAB2").setContent(R.id.widget_layout_red)); //添加第三個標籤頁tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("TAB3").setContent(R.id.widget_layout_green)); //添加監聽tabHost.setOnTabChangedListener(new OnTabChangeListener() {@Overridepublic void onTabChanged(String tabId) { Log.i("Tab", tabId);}});}}
運行後,效果如下: