TabHost是一種非常使用的組件,TabHost可以方便的在窗口上放置多個(gè)標(biāo)簽頁,每個(gè)標(biāo)簽頁相當(dāng)于或得了一個(gè)與外部容器相同大小的組件擺放區(qū)域。
成都創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的柞水網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
與TabHost 結(jié)合使用的組件
TabWidget:代表選項(xiàng)卡的標(biāo)簽條。
TabSpec:代表選項(xiàng)卡的一個(gè)Tab頁面。
創(chuàng)建添加選項(xiàng)卡的方法:
newTabSpec():創(chuàng)建選項(xiàng)卡
addTab():添加選項(xiàng)卡
使用TabHost的步驟:
在界面布局中定義TabHost組件
Activity繼承TabActivity
調(diào)用TabActivity的getTabHost()方法獲取TabHost
通過TabHost對象的方法來創(chuàng)建添加選項(xiàng)卡。
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
LinearLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
android:orientation
=
"vertical"
>
<
TabHost
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
android:id
=
"@android:id/tabhost"
android:layout_weight
=
"1"
>
<
LinearLayout
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
android:orientation
=
"vertical"
>
<
TabWidget
android:id
=
"@android:id/tabs"
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
>
TabWidget
>//代表選項(xiàng)卡的標(biāo)簽條。
<
FrameLayout
android:id
=
"@android:id/tabcontent"
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
>
<
LinearLayout
android:id
=
"@+id/tab01"
android:orientation
=
"vertical"
android:layout_width
=
"fill_parent"
android:layout_height
=
"fill_parent"
>
<
TextView
android:layout_width
=
"fill_parent"
android:layout_height
=
"fill_parent"
android:text
=
"消息"
android:textSize
=
"30sp"
/>
LinearLayout
>
<
LinearLayout
android:id
=
"@+id/tab02"
android:orientation
=
"vertical"
android:layout_width
=
"fill_parent"
android:layout_height
=
"fill_parent"
>
<
TextView
android:layout_width
=
"fill_parent"
android:layout_height
=
"fill_parent"
android:text
=
"賽事"
android:textSize
=
"30sp"
/>
LinearLayout
>
<
LinearLayout
android:id
=
"@+id/tab03"
android:orientation
=
"vertical"
android:layout_width
=
"fill_parent"
android:layout_height
=
"fill_parent"
>
<
TextView
android:layout_width
=
"fill_parent"
android:layout_height
=
"fill_parent"
android:text
=
"我的頁面"
android:textSize
=
"30sp"
/>
LinearLayout
>
FrameLayout
>
LinearLayout
>
TabHost
>
LinearLayout
>
注意:必須使用這樣的,其他的id是錯(cuò)誤的
android:id="@android:id/tabhost"
android:id="@android:id/tabs"
android:id="@android:id/tabcontent"
引用android系統(tǒng)已有的id
public
class
MainActivity
extends
TabActivity{ //Activity繼承TabActivity
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
//
調(diào)用TabActivity的getTabHost()方法獲取TabHost
TabHost tabHost=getTabHost();
//創(chuàng)建第一個(gè)Tab頁
TabHost.TabSpec tab1=tabHost.newTabSpec(
"tab1"
)
.setIndicator(
"賽事"
)
//設(shè)置標(biāo)題
.setContent(R.id.tab01);
//添加第一個(gè)tab頁
tabHost.addTab(tab1);
TabHost.TabSpec tab2=tabHost.newTabSpec(
"tab2"
)
.setIndicator(
"消息"
)
//設(shè)置標(biāo)題
.setContent(R.id.tab02);
//添加第一個(gè)tab頁
tabHost.addTab(tab2);
TabHost.TabSpec tab3=tabHost.newTabSpec(
"tab3"
)
.setIndicator(
"我"
)
//設(shè)置標(biāo)題
.setContent(R.id.tab03);
//添加第一個(gè)tab頁
tabHost.addTab(tab3);
}
}