一、TabHost是什么
TabHost在文档中的定义是:Container for a tabbed window view.直译过来是一个选项卡式窗口视图容器。简单来说,就是类似于微信首页那种,有几个选择卡,点击选择卡可以跳到不同的页面。
二、在了解TabHost之前,有以下知识是需要了解的:
1、TabHost的xml文件中,TabHost、TabWidget和显示TabHost的部分(通常用FrameLayout),这3部分的id是不可修改的,必须使用Android提供的id。
2、TabHost在自定义前,需要初始化,也就是调用tab.setup();(tab是TabHost对象)
3、使用TabHost在几个activity间跳转时,需要继承AcitvityGroup类。并且在初始化时需要调用tab.setup(ActivityGroup.getLocalActivityManager());
4、TabSpec是TabHost的内部类,TabHost需要通过tab.addTab(TabSpec对象)来添加组件,添加的组件之间可以通过TabHost跳转。而实现跳转最重要的是TabSpec的setContent方法,简单来说就是TabSpec的setContent方法传入什么参数,决定TabHost会跳转到哪里。(如果这里不懂没有关系,讲原理的时候会加上源码,到时候就懂了)
三、TabHost有3种实现方式:(通过TabSpec的setContent方法实现)
1、第一种是把需要改变的布局全部写在TabHost的xml中,然后setContent传入id。
2、第二种是在几个Activity间跳转,setContent传入intent对象。
3、第三种是自己定义几种View,然后在这几种View之间跳转。setContent传入TabHost的内部类TabContentFactory的对象。需要重写TabContentFactory的抽象方法。
四、具体使用实现
1、第一种:把需要改变的布局全部写在TabHost的xml中
这种方法是将所有要跳转的布局全部写在TabHost控件中。在给TabHost的对象添加组件时,给TabSpec的setContent方法传入int类型的参数,参数的意义为:要跳转到的布局的id,这个布局为TabHost的子控件。示例如下:
tabspec_int.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- TabHost组件id值不可变-->
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- TabWidget组件id值不可变-->
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
</TabWidget>
<!-- FrameLayout布局,id值不可变-->
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@android:id/tabs">
<!-- 第一个tab的布局 -->
<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一个tab的布局" />
</LinearLayout>
<!-- 第二个tab的布局 -->
<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第二个tab的布局" />
</LinearLayout>
<!-- 第三个tab的布局 -->
<LinearLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第三个tab的布局" />
</LinearLayout>
</FrameLayout>
</RelativeLayout>
</TabHost>
上面的xml中,tabcontent中有3个tab的布局,id分别为tab1、tab2和tab3。在TabspecInt.java中,在给tab.addTab添加组件时,给TabSpec的setContent传入tab1、tab2和tab3的id。
TabspecInt.java
package com.example.tabtest;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TabHost;
public class TabspecInt extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabspec_int);
TabHost tab = (TabHost) findViewById(android.R.id.tabhost);
//初始化TabHost容器
tab.setup();
//在TabHost创建标签,然后设置:标题/图标/标签页布局
tab.addTab(ta