---
title: "TabHost"
author: "Java-Object"
date: "2013-06-01T02:49:13Z"
tags: []
categories: ["编程语言"]
slug: "tabhost"
draft: false
lastmod: "2013-06-01T02:49:13Z"
id: 1880846
url: "https://hezd.iteye.com/blog/1880846"
---

TabHost选项卡

TabHost标签，TabWidget标签，FragmentLayout标签，这三个标签的id都是固定的系统定义的。

TabHost由标签（tabs）和标签体（tabcontent）组成。

 

是一个容器，可以包含并且运行多个嵌入的activity。

已过时，推荐使用Fragment替代。

 

&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout\_width="match\_parent"

    android:layout\_height="match\_parent"

    tools:context=".MainActivity" &gt;

 

    &lt;TabHost

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

        android:layout\_width="match\_parent"

        android:layout\_height="match\_parent" &gt;

 

        &lt;TabWidget

            android:id="@android:id/tabs"

            android:layout\_width="wrap\_content"

            android:layout\_height="wrap\_content" &gt;

        &lt;/TabWidget&gt;

 

        &lt;FrameLayout

            android:id="@android:id/tabcontent"

            android:layout\_width="wrap\_content"

            android:layout\_height="wrap\_content" &gt;

        &lt;/FrameLayout&gt;

    &lt;/TabHost&gt;

 

&lt;/RelativeLayout&gt;

 

创建TabHost步骤：

1.获取当前activity中的TabHost，getTabHost。

2.创建一个标签TabSpec对象，它里面封装了标签指示符（卡片）和标签体（显示内容），指定标签的详细说明TabSpec ，TabSpec用来指定如何创建标签指示符和标签卡内容。tabHost.addTab(tabspec); TabSpec = TabHost.newTabSpace(tabname);

3.设置标签指示符和标签内容，标签内容是一个意图对象，用来开启一个activity。

4.将标签TabSpec添加到TabHost选项卡中。

 

---------------------------代码示例-----------------

1.标签卡xml文件

&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout\_width="match\_parent"

    android:layout\_height="match\_parent"

    tools:context=".MainActivity" &gt;

 

    &lt;TabHost

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

        android:layout\_width="match\_parent"

        android:layout\_height="match\_parent" &gt;

 

        &lt;TabWidget

            android:id="@android:id/tabs"

            android:layout\_width="match\_parent"

            android:layout\_height="wrap\_content" &gt;

        &lt;/TabWidget&gt;

 

        &lt;FrameLayout

            android:id="@android:id/tabcontent"

            android:layout\_width="match\_parent"

            android:layout\_height="match\_parent" &gt;

        &lt;/FrameLayout&gt;

    &lt;/TabHost&gt;

 

&lt;/RelativeLayout&gt;

 

2.TabActivity类

package com.itheima.tabhost;

 

import android.os.Bundle;

import android.app.Activity;

import android.app.TabActivity;

import android.content.Intent;

import android.view.Menu;

import android.widget.TabHost;

import android.widget.TabHost.TabSpec;

 

public class MainActivity extends TabActivity {

 

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity\_main);

 

TabHost tabHost = getTabHost();

 

TabSpec tab1 = tabHost.newTabSpec("标签1");

TabSpec tab2 = tabHost.newTabSpec("标签2");

TabSpec tab3 = tabHost.newTabSpec("标签3");

 

tab1.setIndicator("功能1");

tab1.setContent(new Intent(this,activity01.class));

 

tab2.setIndicator("功能2");

tab2.setContent(new Intent(this,activity02.class));

 

tab3.setIndicator("功能3");

tab3.setContent(new Intent(this,activity03.class));

 

tabHost.addTab(tab1);

tabHost.addTab(tab2);

tabHost.addTab(tab3);

}

 

}

3.activity01类

package com.itheima.tabhost;

 

import android.app.Activity;

import android.os.Bundle;

 

public class activity01 extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

 

setContentView(R.layout.tab1\_layout);

}

}

 

4.activity01的xml布局文件

 

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

&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout\_width="match\_parent"

    android:layout\_height="match\_parent"

     &gt;

 

   &lt;TextView

       android:gravity="center"

        android:layout\_width="match\_parent"

        android:layout\_height="match\_parent"

        android:textSize="20sp"

        android:text="我是标签1" /&gt;

 

&lt;/RelativeLayout&gt;