Android 控件之TabHost Tab页

TabHost用来显示Tab页,先看效果

 

源码下载

一概述

    提供Tab页的窗口视图容器,它有俩个children,一组是用户可以选择指定Tab页的标签,另一组是FrameLayout用来显示该Tab页的内容。个别元素通常控制使用这个容器对象,而不是设置在子元素本身的值。

二、重要方法

    addTab(TabHost.TabSpec tabSpec):添加一项Tab页

    clearAllTabs():清除所有与之相关联的Tab页.

    getCurrentTab():返回当前Tab页.

    getTabContentView():返回包含内容的FrameLayout

    newTabSpec(String tag):返回一个与之关联的新的TabSpec

三、实例

1.布局文件,需要使用FrameLayout

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView android:id="@+id/view1"
        android:background="@drawable/b"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="页1"/>

    <TextView android:id="@+id/view2"
        android:background="@drawable/c"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="页2"/>

    <TextView android:id="@+id/view3"
        android:background="@drawable/d"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="页3"/>

</FrameLayout>

 2.继承TabActivity

public class TabHostDemo extends TabActivity

3.获取次此abHost

 TabHost tabHost = getTabHost();

4.设置布局

LayoutInflater.from(this).inflate(R.layout.tabhostpage, tabHost.getTabContentView(), true);

5.添加Tab页

 tabHost.addTab(tabHost.newTabSpec("tab1")
                 .setIndicator("tab1")
                 .setContent(R.id.view1));
         tabHost.addTab(tabHost.newTabSpec("tab3")
                 .setIndicator("tab2")
                 .setContent(R.id.view2));
         tabHost.addTab(tabHost.newTabSpec("tab3")
                 .setIndicator("tab3")
                 .setContent(R.id.view3));

 

    

posted @ 2010-10-07 11:50  ForrestWoo  阅读(28595)  评论(2编辑  收藏  举报