---
title: "TabHost用法—定义Tab标签样式"
author: "jguangyou"
date: "2012-02-28T11:36:09Z"
tags: []
categories: ["移动开发"]
slug: "tabhost-tab"
draft: false
lastmod: "2012-04-19T10:23:42Z"
id: 1428803
url: "https://deep-fish.iteye.com/blog/1428803"
---

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

在上一篇文章“TabHost用法”中我们介绍了通过TabHost实现标签页效果。但是在实际项目中我们可能更希望定义自己的Tab标签样式使界面效果更佳。既然不能改变系统的Tab样式，那么我们可以选择隐藏系统的东西，使用自己定义的东西（这种方式很好用，以后会详细介绍）。反编译新浪微博的项目后会发现，他们在布局中隐藏了TabWidget即Tab标签而使用一组RadioButton来代替。既然是自己定义的，那肯定是可以自己决定显示样式了，那我们的问题也就解决了。这里我使用的是“TabHost用法—两种实现方式”一文种提到的第二种实现方式，继承Activity来使用TabHost的。先把代码贴上来（红色字体部分为修改或添加的代码）。

**TabHostActivity.java**

**public** **class** TabHostActivity **extends** Activity **implements**

OnCheckedChangeListener {

**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);

setContentView(R.layout.*tab*);

// tabHost = getTabHost();

tabHost = (TabHost) findViewById(R.id.*my\_tabhost*);

LocalActivityManager groupActivity =

**new** LocalActivityManager(**this**,**false**);

groupActivity.dispatchCreate(savedInstanceState);

tabHost.setup(groupActivity);

initIntent();

addSpec();

**((RadioGroup) findViewById(R.id.*tab\_radiogroup*))**

**.set[]()[]()OnCheckedChangeListener(this);**

}

/\**

\* 初始化各个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.*account04*,

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);

}

**@Override**

**public void onCheckedChanged(RadioGroup group, int checkedId) {**

**switch (checkedId) {**

**case R.id.*radio\_button\_study*:**

**tabHost.setCurrentTabByTag("tab\_study");**

**break;**

**case R.id.*radio\_button\_score*:**

**tabHost.setCurrentTabByTag("tab\_score");**

**break;**

**case R.id.*radio\_button\_certificate*:**

**tabHost.setCurrentTabByTag("tab\_certificate");**

**break;**

**case R.id.*radio\_button\_fee*:**

**tabHost.setCurrentTabByTag("tab\_fee");**

**break;**

**case R.id.*radio\_button\_more*:**

**tabHost.setCurrentTabByTag("tab\_more");**

**break;**

**}**

**}**

}

**tab.xml**

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

&lt;TabHost android:id=*"@+id/my\_tabhost"* android:layout\_width=*"fill\_parent"*

android:layout\_height=*"fill\_parent"* xmlns:android=*"http://schemas.android.com/apk/res/android"*&gt;

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

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

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

android:layout\_width=*"fill\_parent"* android:layout\_height=*"0.0dip"*

android:layout\_weight=*"1.0"* /&gt;

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

android:layout\_width=*"fill\_parent"* android:layout\_height=*"wrap\_content"*

android:layout\_weight=*"0.0"* /&gt;

**&lt;RadioGroup android:id=*"@+id/tab\_radiogroup"***

**android:background=*"@drawable/tabs\_bg"* android:layout\_width=*"fill\_parent"***

**android:layout\_height=*"wrap\_content"* android:gravity=*"center\_vertical"***

**android:layout\_gravity=*"bottom"* android:orientation=*"horizontal"*&gt;**

&lt;RadioButton android:id=*"@+id/radio\_button\_study"*

android:layout\_marginTop=*"2.0dip"* android:text=*"**学习进度"*

android:drawableTop=*"@drawable/account01"* style=*"@style/tab\_button\_bottom"*

android:checked=*"true"* /&gt;

&lt;RadioButton android:id=*"@+id/radio\_button\_score"*

android:layout\_marginTop=*"2.0dip"* android:text=*"**考试成绩"*

android:drawableTop=*"@drawable/account02"* style=*"@style/tab\_button\_bottom"* /&gt;

&lt;RadioButton android:id=*"@+id/radio\_button\_certificate"*

android:layout\_marginTop=*"2.0dip"* android:text=*"**证书发放"*

android:drawableTop=*"@drawable/account03"* style=*"@style/tab\_button\_bottom"* /&gt;

&lt;RadioButton android:id=*"@+id/radio\_button\_fee"*

android:layout\_marginTop=*"2.0dip"* android:text=*"**费用缴纳"*

android:drawableTop=*"@drawable/account04"* style=*"@style/tab\_button\_bottom"* /&gt;

&lt;RadioButton android:id=*"@+id/radio\_button\_more"*

android:layout\_marginTop=*"2.0dip"* android:text=*"**更多"*

android:drawableTop=*"@drawable/account05"* style=*"@style/tab\_button\_bottom"* /&gt;

&lt;/RadioGroup&gt;

&lt;/LinearLayout&gt;

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

**styles.xml**

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

&lt;resources&gt;

&lt;!-- TabHost标签按钮样式 --&gt;

&lt;style name=*"tab\_button\_bottom"*&gt;

&lt;item name=*"android:textSize"*&gt;12px&lt;/item&gt;

&lt;item name=*"android:textColor"*&gt;#ffffffff&lt;/item&gt;

&lt;item name=*"android:ellipsize"*&gt;marquee&lt;/item&gt;

&lt;item name=*"android:gravity"*&gt;center\_horizontal&lt;/item&gt;

&lt;item name=*"android:background"*&gt;@drawable/tab\_btn\_bg&lt;/item&gt;

&lt;item name=*"android:layout\_marginTop"*&gt;2.0dip&lt;/item&gt;

&lt;item name=*"android:button"*&gt;@null&lt;/item&gt;

&lt;item name=*"android:paddingTop"*&gt;6dip&lt;/item&gt;

&lt;item name=*"android:drawablePadding"*&gt;4dip&lt;/item&gt;

&lt;item name=*"android:layout\_width"*&gt;fill\_parent&lt;/item&gt;

&lt;item name=*"android:layout\_height"*&gt;wrap\_content&lt;/item&gt;

&lt;item name=*"android:layout\_weight"*&gt;1.0&lt;/item&gt;

&lt;item name=*"android:singleLine"*&gt;true&lt;/item&gt;

&lt;/style&gt;

&lt;!-- 页面标题LinearLayout样式 --&gt;

&lt;style name=*"activity\_title\_background"*&gt;

&lt;item name=*"android:background"*&gt;@drawable/title\_background&lt;/item&gt;

&lt;item name=*"android:layout\_width"*&gt;fill\_parent&lt;/item&gt;

&lt;item name=*"android:layout\_height"*&gt;wrap\_content&lt;/item&gt;

&lt;item name=*"android:layout\_alignParentTop"*&gt;true&lt;/item&gt;

&lt;item name=*"android:gravity"*&gt;center&lt;/item&gt;

&lt;/style&gt;

&lt;!-- 界面标题TextView样式 --&gt;

&lt;style name=*"activity\_title\_text"*&gt;

&lt;item name=*"android:textSize"*&gt;14dip&lt;/item&gt;

&lt;item name=*"android:textColor"*&gt;@drawable/white&lt;/item&gt;

&lt;item name=*"android:layout\_width"*&gt;wrap\_content&lt;/item&gt;

&lt;item name=*"android:layout\_height"*&gt;wrap\_content&lt;/item&gt;

&lt;item name=*"android:gravity"*&gt;center&lt;/item&gt;

&lt;/style&gt;

&lt;/resources&gt;

运行结果如下图所示

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

 

程序重要部分：

1\. 红色字体部分。

2\. 布局文件tab.xml,可以看到该布局文件中将TabWidget隐藏（**android:visibility=*"gone"***）而以一个RadioGroup取而代之。

3\. 为RadioGroup设置**OnCheckedChangeListener**监听，通过**onCheckedChanged**方法对各个RadioButton点击事件的处理完成标签切换。

*其实我当初考虑过为什么要用RadioButton而不用普通的Button。后来通过自己做项目，发现使用RadioGroup有以下优点：*

*1.*另外就是布局上比较方便易懂，不用再去用LinearLayout等布局去包含Button。

*2.* 我们可以很方便的获得当前选中的标签，当然通过TabHost的tabHost.getCurrentTabTag()和getCurrentTab()也是可以的。

*3.*设置监听很方便，只需要为RadioGroup设置监听就行了，程序中对应的代码是

**((RadioGroup) findViewById(R.id.*tab\_radiogroup*))**

**.setOnCheckedChangeListener(this);**

如果用Button的话我们需要为所有的Button一个一个去设置监听，相对来说比较麻烦。

*4. 或许最重要的一点是因为RadioButton本身就支持图片和文字的上下布局，只需指定图片和文字是什么就可以了而不需要我们自己去实现这种布局。*

&lt;RadioButton android:id=*"@+id/radio\_button\_more"*

android:layout\_marginTop=*"2.0dip"*

android:text=*"**更多"*

android:drawableTop=*"@drawable/account05"*

style=*"@style/tab\_button\_bottom"* /&gt;

*当然如果如果RadioButton不能满足我们的项目需求，比如我们不需要图片又不想让文字靠底部显示，而是居中显示，这时我们就可以用其他组件代替RadioButton。其实我们可以通过修改或自定义等方式实现多种漂亮的效果，比如“人人网”手机客户端的个人主页中Tab标签是可以左右滑动的。*