本文结合源代码和实例来说明TabHost的用法。

使用TabHost 可以在一个屏幕间进行不同版面的切换,例如android自带的拨号应用,截图:

查看tabhost的源代码,主要实例变量有:

private TabWidget mTabWidget;
    private FrameLayout mTabContent;
    private List<TabSpec> mTabSpecs
   也就是说我们的tabhost必须有这三个东西,所以我们的.xml文件就会有规定:继续查看源代码:

if (mTabWidget == null) {
            throw new RuntimeException(
                    "Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabs'");
        }

mTabContent = (FrameLayout) findViewById(com.android.internal.R.id.tabcontent);
        if (mTabContent == null) {
            throw new RuntimeException(
                    "Your TabHost must have a FrameLayout whose id attribute is 'android.R.id.tabcontent'");
        }
     也就是说我们的.xml文件需要TabWidget和FrameLayout标签。

接下来构建我们自己的tab实例:

有两种方式可以实现:
      一种是继承TabActivity 类,可以使用android的自己内部定义好的.xml资源文件作容器文件。也就是在我们的代码中使用getTabHost(); , 而相应的后台源码是这样的:

this.setContentView(com.android.internal.R.layout.tab_content);
       在系统的资源文件中可以看见这个layout

有了容器,然后我们就需要我们为每个tab分配内容,当然要可以是如何类型的标签:
      例如我们构建一下.xml文件
  首先tab1.xml 是一个LinearLayout布局

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout01" android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <TextView android:text="tab1 with linear layout"
        android:id="@+id/TextView01" android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </TextView>
</LinearLayout>

然后是tab2.xml是一个FrameLayout布局

<?xml version="1.0" encoding="utf-8"?>
    <FrameLayout  xmlns:android="http://schemas.android.com/apk/res/android"
   
    android:id="@+id/FrameLayout02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <LinearLayout android:id="@+id/LinearLayout02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <TextView android:text="tab2"
                android:id="@+id/TextView01" android:layout_width="wrap_content"
                android:layout_height="wrap_content">
            </TextView>
        </LinearLayout>
       
    </FrameLayout>
接着要注册这两个FrameLayout为tabhost的Content,也就是接下来的代码:

LayoutInflater inflater_tab1 = LayoutInflater.from(this);  
        inflater_tab1.inflate(R.layout.tab1, mTabHost.getTabContentView()); 
        inflater_tab1.inflate(R.layout.tab2, mTabHost.getTabContentView());
 
然后需要构建前面说的tabhost的第三个实例变量对应得内容,源代码中是这样的:

private List<TabSpec> mTabSpecs = new ArrayList<TabSpec>(2);
 初始化是两个tab的空间然后会自动扩展:
好 我们构建我们的tabspec:

mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB 11").setContent(R.id.LinearLayout01)); 
        mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB 11").setContent(R.id.FrameLayout02));

也就是把我们的2个layout作为他的content,当然FrameLayout中可以有其他的布局,来放我的组件。
我们不需要在代码里面设置setContentView();因为getTabHost(); 这个方法调用后就已经设置了,源代码:

if (mTabHost == null) {
            this.setContentView(com.android.internal.R.layout.tab_content);
        }
也就是把系统的tab_content当做view设置。
运行后如下:
 
完整代码:

TabHost mTabHost = getTabHost();
        LayoutInflater inflater_tab1 = LayoutInflater.from(this);  
        inflater_tab1.inflate(R.layout.tab1, mTabHost.getTabContentView()); 
        inflater_tab1.inflate(R.layout.tab2, mTabHost.getTabContentView());  
        mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB 11").setContent(R.id.LinearLayout01)); 
        mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB 11").setContent(R.id.FrameLayout02));

还有一种就是定义我们自己的tabhost:不用继承TabActivity

首先建立我们自己的.xml文件,当然要包含Tabhost,TabWidget,FrameLayout,着3个标签:

<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/tabhost" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <LinearLayout 
        android:orientation="vertical" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"> 
        <TabWidget 
            android:id="@android:id/tabs" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" /> 
        <FrameLayout 
            android:id="@android:id/tabcontent" 
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent"> 
            
        </FrameLayout> 
    </LinearLayout> 
</TabHost>

注意的是:除了tabhost的id可以自定义外,其他的必须使用系统的id,为什么后面说,
       当然我们可以在FrameLayout里面添加view来作为tab的内容只需要在create tabspce时候添加就可以了,我们为了把每个tab的内容分开我们依然使用前面用到的两个tab xml文件
 java代码:
      获取TabHost 通过findviewbyid,

setContentView(R.layout.main);  
        TabHost mTabHost = (TabHost)findViewById(R.id.tabhost);
    接下来很重要的一步是要使用TabHost.setup();
     作用是来初始化我们的TabHost容器:
    源代码是这样说的:

<p>Call setup() before adding tabs if loading TabHost using findViewById(). <i><b>However</i></b>: You do
      * not need to call setup() after getTabHost() in {@link android.app.TabActivity TabActivity}.
  也就是说通过findviewbyid,方法获得tabhost必须setup 而通过getTabHost则不用。
  setup干什么呢:源代码

mTabWidget = (TabWidget) findViewById(com.android.internal.R.id.tabs);
        if (mTabWidget == null) {
            throw new RuntimeException(
                    "Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabs'");
        }

mTabContent = (FrameLayout) findViewById(com.android.internal.R.id.tabcontent);
        if (mTabContent == null) {
            throw new RuntimeException(
                    "Your TabHost must have a FrameLayout whose id attribute is 'android.R.id.tabcontent'");
        }
   他主要是初始化了tabhost的两个实例变量,这里也回答了为什么我们的id必须使用系统定义的id的原因
  接下来工作就和前面相同了:

LayoutInflater inflater_tab1 = LayoutInflater.from(this);  
        inflater_tab1.inflate(R.layout.tab1, mTabHost.getTabContentView()); 
        inflater_tab1.inflate(R.layout.tab2, mTabHost.getTabContentView());
        mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB a").setContent(R.id.LinearLayout01));  
        mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("TAB b").setContent(R.id.FrameLayout02));

完整代码:

setContentView(R.layout.main);  
        TabHost mTabHost = (TabHost)findViewById(R.id.tabhost);
        mTabHost.setup();
        LayoutInflater inflater_tab1 = LayoutInflater.from(this);  
        inflater_tab1.inflate(R.layout.tab1, mTabHost.getTabContentView()); 
        inflater_tab1.inflate(R.layout.tab2, mTabHost.getTabContentView());
        mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB a").setContent(R.id.LinearLayout01));  
        mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("TAB b").setContent(R.id.FrameLayout02));

运行结果同上。 如有问题欢迎提出。

转载请说明出处。。。

加上源代码,有用了可以下载下:/Files/freeman1984/atab.rar

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/lastsweetop/archive/2010/05/07/5566200.aspx

android Tabhost部件相关推荐

  1. Android Tabhost with FragmentActivity

    此文解决我这两天的问题,故转载:原文Android Tabhost with FragmentActivity   2012-05-07 更新)接續Android TabHost中切換Activity ...

  2. Android TabHost中切換、修改需要显示的Activity

    上個星期跟盛哥試了一段時間使用Fragment後還是不得其門而入(兩個人對Fragment都還不太熟悉),卡住的原因是現在有兩個Tab,當Tab1進入到第二個畫面,Tab2進到第三個畫面,但使用者切回 ...

  3. Android TabHost和xml定义Menu应用

    Android TabHost和xml定义Menu应用 http://files.cnblogs.com/hnrainll/TabMenu.zip

  4. surface android模拟,Surface Duo将支持Android小部件 模拟磁铁体验

    中关村在线消息:根据国外媒体曝光的信息,即将发售的双屏智能手机Surface Duo将支持Android的交互式小部件,以满足喜欢Windows Phone上的实时磁贴的用户的需求. 据悉,用户可以将 ...

  5. 使用Eclipse和Android小部件进行Android开发的简介

    Android是一种移动操作系统,类似于Symbian,iOS,Windows®Mobile等. 它最初由Android Inc.开发,后来被Google收购. 它现在由开放手机联盟(Open Han ...

  6. android tabhost --android UI 学习

    2019独角兽企业重金招聘Python工程师标准>>> 实现TabHost有三种方式:继承自TabActivity,ActivityGroup和自定义的Activity 实现效果图: ...

  7. 【Android 应用开发】Android - TabHost 选项卡功能用法详解

    TabHost效果图 : 源码下载地址 : http://download.csdn.net/detail/han1202012/6845105        . 作者 :万境绝尘  转载请注明出处  ...

  8. android小部件如何实时更新,android – 使用AlarmManager手动更新小部件

    如 Android Dev Guide中所述,如果您希望窗口小部件更频繁地更新,则应使用AlarmManager设置不唤醒设备的警报. 原则上:不要使用AppWidgetProvider类提供的标准机 ...

  9. Android --- TabHost 切换时,改变选项卡下字体的状态(大小、加粗、默认被选中第一个)

    上效果图: MiddleFragment.java 代码如下 import android.os.Bundle; import android.view.LayoutInflater; import ...

最新文章

  1. iOS CoreBluetooth 教程
  2. CenterOS 服务器之MySQL卸载与安装,并远程连接
  3. aria-label及aria-labelledby应用//////////[信息无障碍产品联盟]
  4. centos搭建rsync服务!
  5. opencv配置中常见问题
  6. Excel关于宏的运用
  7. Cocos2d-x场景(Scene)详解
  8. php算数组内值的总和,怎样使用array_sum() 计算数组元素值总和
  9. QT集成Windows手写输入法
  10. ArcGIS网络分析扩展模块
  11. 用Python把PDF文件转换成Word文档
  12. 【Linux】Ubuntu运行环境搭建
  13. 【Day 3】机器阅读理解——常见机器阅读理解模型(下)
  14. 计算机玩电脑游戏,玩电脑游戏250字
  15. P5.JS绘制动态图形
  16. java与前端实现7种二维码
  17. 深度学习(6)之卷积的几种方式:1D、2D和3D卷积的不同卷积原理(全网最全!)
  18. vb.net mysql 查询,mysql-vb.net查询以显示数据表的特定行[基本]
  19. 中国互联网生态报告发布
  20. ML (Chapter 10): 降维与度量学习

热门文章

  1. swift (Singleton)模式
  2. 前端开发神器之ngrok
  3. 从游戏脚本语言说起,剖析Mono所搭建的脚本基础
  4. 区块链学堂:区块链引子
  5. alexa技能个数_如何在您的技能中使用Alexa演示语言
  6. php 命令执行crud_如何使用原始JavaScript执行CRUD操作
  7. 手动部署OpenStack环境(一:Virtual Box 5.1 环境的安装及配置)
  8. L1-025 正整数A+B
  9. ssm框架实现学生成绩管理系统
  10. PAT(甲级)2018年秋季考试 7-1 Werewolf - Simple Version