---
title: "TabHost用法"
author: "jguangyou"
date: "2012-02-28T11:20:53Z"
tags: []
slug: "tabhost"
draft: false
lastmod: "2012-02-28T11:20:53Z"
id: 1428782
url: "https://deep-fish.iteye.com/blog/1428782"
---

***注：该文章为（男人应似海）原创，如需转载请注明出处！***

通常情况下我们会通过继承TabActivity，调用getTabHost()获取[]()[]()TabHost实例,下面是具体过程。

**TabHostActivity.java**

**public** **class** TabHostActivity **extends** TabActivity {

**private** TabHost tabHost;

**private** Intent certificateIntent;

**private** Intent feeIntent;

**private** Intent scoreIntent;

**private** Intent studyIntent;

**private** Intent moreIntent;

@Override

**publicvoid** onCreate(Bundle savedInstanceState) {

**super**.onCreate(savedInstanceState);

tabHost = getTabHost();

initIntent();

addSpec();

}

/\**

\* 初始化各个tab标签对应的intent

\*/

**privatevoid** initIntent() {

studyIntent = **new** Intent(**this**, StudyActivity.**class**);

scoreIntent = **new** Intent(**this**, ScoreActivity.**class**);

feeIntent = **new** Intent(**this**, FeeActivity.**class**);

certificateIntent = **new** Intent(**this**, CertificateActivity.**class**);

moreIntent = **new** Intent(**this**, MoreActivity.**class**);

}

/\**

\* 为tabHost添加各个标签项

\*/

**privatevoid** addSpec() {

tabHost.addTab(**this**.buildTagSpec("tab\_study",

R.string.*study\_progress*,R.drawable.*account01*, studyIntent));

tabHost.addTab(**this**.buildTagSpec("tab\_score",

R.string.*test\_score*,R.drawable.*account02*, scoreIntent));

tabHost.addTab(**this**.buildTagSpec("tab\_fee",

R.string.*fee\_pay*,R.drawable.*account03*, feeIntent));

tabHost.addTab(**this**.buildTagSpec("tab\_certificate", R.string.*certificate\_grant*,R.drawable.*account*certificateIntent));

tabHost.addTab(**this**.buildTagSpec("tab\_more", R.string.*more*,

R.drawable.*account05*, moreIntent));

}

/\**

\* 自定义创建标签项的方法

\* **@param** tagName 标签标识

\* **@param** tagLable 标签文字

\* **@param** icon 标签图标

\* **@param** content 标签对应的内容

\* **@return**

\*/

**private** TabHost.TabSpec buildTagSpec(String tagName, **int** tagLable,

**int** icon, Intent content) {

**return**tabHost

.newTabSpec(tagName)

.setIndicator(getResources().getString(tagLable),

getResources().getDrawable(icon)).setContent(content);

}}

运行结果如下图所示

 

![](http://pic002.cnblogs.com/images/2011/282238/2011101211414419.png)

 

 

我们发现标签位置处于界面上方，但是我们看到的很多应用的标签都处于界面底部。

如下图所示

 

![](http://pic002.cnblogs.com/images/2011/282238/2011101211421810.png)

 

 

我们要实现这种效果只需要[]()[]()将TabActivity的默认布局覆盖即可。新布局只需将标签和标签对应内容的相对位置调换一下就可以了，这里是用相对布局将标签对应内容的位置放到了标签的上方。不要改动id（会抛异常，提示必须要用指定的id）。不要忘了在onCreate()里设置新布局将TabActivity的默认布局覆盖。

@Override

**publicvoid** onCreate(Bundle savedInstanceState) {

**super**.onCreate(savedInstanceState);

setContentView(R.layout.tab);

tabHost = getTabHost();

initIntent();

addSpec();

}

**tab.xml**

&lt;?xml version=*"1.0"* encoding=*"UTF-8"*?&gt;

&lt;!-- TabHost组件id值不可变--&gt;

&lt;[]()[]()TabHostxmlns:android=[*http://schemas.android.com/apk/res/android*](http://schemas.android.com/apk/res/android)

android:id=*"@android:id/tabhost"*

android:layout\_height=*"fill\_parent"*

android:layout\_width=*"fill\_parent"*&gt;

&lt;RelativeLayout android:orientation=*"vertical"*

android:layout\_width=*"fill\_parent"*

android:layout\_height=*"fill\_parent"*&gt;

&lt;!-- TabWidget组件id值不可变--&gt;

&lt;TabWidget android:id=*"@android:id/tabs"*

android:layout\_width=*"fill\_parent"*

android:layout\_height=*"wrap\_content"*

android:layout\_alignParentBottom=*"true"*&gt;

&lt;/TabWidget&gt;

&lt;!-- FrameLayout布局，id值不可变--&gt;

&lt;FrameLayout android:id=*"@android:id/tabcontent"*

android:layout\_width=*"fill\_parent"*

android:layout\_height=*"fill\_parent"*

android:layout\_above=*"@android:id/tabs"*&gt;

&lt;/FrameLayout&gt;

&lt;/RelativeLayout&gt;

**&lt;/TabHost&gt;**

 

*通常在项目中我们都会有一个自定义的Activity基类，我们会让所有的界面Activity去继承这个基类。但是要使用TabHost就要继承TabActivity，所以我们可以定义两个基类，一个是普通Activity界面的基类，另一个是包含TabHost界面的基类，让这个基类继承TabActivity即可。*