TabHost主要特点是能够在一个窗体中显示多组标签栏的内容,在Android系统之中每一个标签栏就称为一个Tab。而包括这多个标签栏的容器就将其称为TabHost。TabHost类的继承结构例如以下所看到的:
java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.FrameLayout
         ↳ android.widget.TabHost 

经常用法例如以下所看到的
1
public TabHost(Context context)
构造
创建TabHost类对象
2
public void addTab(TabHost.TabSpec tabSpec)
普通
添加一个Tab
3
public TabHost.TabSpec newTabSpec(String tag)
普通
创建一个TabHost.TabSpec对象
4
public View getCurrentView()
普通
取得当前的View对象
5
public void setup()
普通
建立TabHost对象
6
public void setCurrentTab(int index)
普通
设置当前显示的Tab编号
7
public void setCurrentTabByTag(String tag)
普通
设置当前显示的Tab名称
8
public FrameLayout getTabContentView()
普通
返回标签容器
9
public void setOnTabChangedListener
(TabHost.OnTabChangeListener l)
普通
设置标签改变时触发

两种方式实现TabHost

方式一:直接让一个Activity程序继承TabActivity类。
方式二:利用findViewById()方法取得TagHost组件。并进行若干配置。

第一种方式让一个类继承tabActivity

XMl文件 配置须要在一个xml文件里嵌套使用布局 来达到不同Tab中显示不同的内容

  1. <?
  2.  
  3. xml version="1.0" encoding="utf-8"?>
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical" >
  8.  
  9. <LinearLayout
  10. android:layout_marginTop="50dp"
  11. android:id="@+id/login"
  12. android:layout_width="match_parent"
  13. android:layout_height="match_parent"
  14. android:orientation="vertical" >
  15.  
  16. <TableRow
  17. android:id="@+id/tableRow1"
  18. android:layout_width="match_parent"
  19. android:layout_height="wrap_content" >
  20.  
  21. <TextView
  22. android:id="@+id/textView1"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:text="账号" />
  26.  
  27. <EditText
  28. android:id="@+id/editText1"
  29. android:layout_width="wrap_content"
  30. android:layout_height="wrap_content"
  31. android:ems="10"
  32. android:textSize="25sp" />
  33. </TableRow>
  34.  
  35. <TableRow
  36. android:id="@+id/tableRow2"
  37. android:layout_width="match_parent"
  38. android:layout_height="wrap_content" >
  39.  
  40. <TextView
  41. android:id="@+id/textView2"
  42. android:layout_width="wrap_content"
  43. android:layout_height="wrap_content"
  44. android:text="password" />
  45.  
  46. <EditText
  47. android:id="@+id/editText2"
  48. android:layout_width="wrap_content"
  49. android:layout_height="wrap_content"
  50. android:ems="10"
  51. android:textSize="25sp" />
  52. </TableRow>
  53.  
  54. <TableRow
  55. android:id="@+id/tableRow3"
  56. android:layout_width="match_parent"
  57. android:layout_height="wrap_content" >
  58.  
  59. <Button
  60. android:id="@+id/button1"
  61. android:layout_width="wrap_content"
  62. android:layout_height="wrap_content"
  63. android:layout_weight="1"
  64. android:text="取消" />
  65.  
  66. <Button
  67. android:id="@+id/button2"
  68. android:layout_width="wrap_content"
  69. android:layout_height="wrap_content"
  70. android:layout_weight="1"
  71. android:text="登录" />
  72. </TableRow>
  73. </LinearLayout>
  74.  
  75. <LinearLayout
  76. android:layout_marginTop="50dp"
  77. android:id="@+id/image"
  78. android:layout_width="match_parent"
  79. android:layout_height="match_parent"
  80. android:orientation="vertical" >
  81.  
  82. <ImageView
  83. android:layout_width="wrap_content"
  84. android:layout_height="wrap_content"
  85. android:src="@drawable/a2" />
  86. </LinearLayout>
  87.  
  88. <LinearLayout
  89. android:layout_marginTop="50dp"
  90. android:id="@+id/timer"
  91. android:layout_width="match_parent"
  92. android:layout_height="match_parent"
  93. android:orientation="vertical" >
  94.  
  95. <TimePicker
  96. android:layout_width="match_parent"
  97. android:layout_height="wrap_content" />
  98. </LinearLayout>
  99. <LinearLayout
  100. android:layout_marginTop="50dp"
  101. android:id="@+id/timer1"
  102. android:layout_width="match_parent"
  103. android:layout_height="match_parent"
  104. android:orientation="vertical" >
  105.  
  106. <TimePicker
  107. android:layout_width="match_parent"
  108. android:layout_height="wrap_content" />
  109. </LinearLayout>
  110. <LinearLayout
  111. android:layout_marginTop="50dp"
  112. android:id="@+id/timer2"
  113. android:layout_width="match_parent"
  114. android:layout_height="match_parent"
  115. android:orientation="vertical" >
  116.  
  117. <TimePicker
  118. android:layout_width="match_parent"
  119. android:layout_height="wrap_content" />
  120. </LinearLayout>
  121. </LinearLayout>

JAVA文件设置


  1. package com.example.tabhost;
  2.  
  3. import android.app.TabActivity;
  4. import android.os.Bundle;
  5. import android.view.LayoutInflater;
  6. import android.widget.TabHost;
  7. import android.widget.TabHost.TabSpec;
  8.  
  9. public class MainActivity extends TabActivity {
  10. private TabHost tabHost;//初始化TabHost组件
  11. private int reslayout[] = { R.id.login, R.id.image, R.id.timer , R.id.timer1,R.id.timer2};//设置相应的额xml文件
  12. private int images[]={R.drawable.cart,R.drawable.cloud,R.drawable.comment,R.drawable.gear,R.drawable.joystick};//设置显示的标题文件
  13.  
  14. @SuppressWarnings("deprecation")
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. this.tabHost = super.getTabHost();//实例化TabHost组件
  19. // 取得LayoutInflater对象
  20. LayoutInflater.from(this).inflate(R.layout.linearlayout,//制定布局
  21. this.tabHost.getTabContentView(),//制定标签添加的容器
  22. true);
  23.  
  24. for (int i = 0; i < reslayout.length; i++) {
  25. TabSpec myTab = tabHost.newTabSpec("tab" + i);// 定义TabSpec
  26. myTab.setIndicator(null,getResources().getDrawable(images[i])) ; // 设置标签
  27.  
  28. myTab.setContent(this.reslayout[i]) ; // 设置显示的组件
  29. this.tabHost.addTab(myTab) ;
  30. }
  31. }
  32.  
  33. }

效果图例如以下







使用配置文件设置。
这样的方法较为常见,能够讲TabHost置于底部

可是布局文件较为复杂,大家能够參照样例进行详细的学习

XML文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TabHost
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:id="@+id/tabhost"
  5. android:orientation="vertical"
  6. android:layout_width="fill_parent"
  7. android:layout_height="fill_parent">
  8. <RelativeLayout
  9. android:orientation="vertical"
  10. android:layout_width="fill_parent"
  11. android:layout_height="fill_parent">
  12. <TabWidget
  13. android:id="@android:id/tabs"
  14. android:layout_width="fill_parent"
  15. android:layout_height="wrap_content"
  16. android:layout_alignParentBottom="true" />
  17. <FrameLayout
  18. android:id="@android:id/tabcontent"
  19. android:layout_width="fill_parent"
  20. android:layout_height="fill_parent">
  21. <LinearLayout
  22.  
  23. android:id="@+id/login"
  24. android:layout_width="match_parent"
  25. android:layout_height="match_parent"
  26. android:orientation="vertical" >
  27.  
  28. <TableRow
  29. android:id="@+id/tableRow1"
  30. android:layout_width="match_parent"
  31. android:layout_height="wrap_content" >
  32.  
  33. <TextView
  34. android:id="@+id/textView1"
  35. android:layout_width="wrap_content"
  36. android:layout_height="wrap_content"
  37. android:text="账号" />
  38.  
  39. <EditText
  40. android:id="@+id/editText1"
  41. android:layout_width="wrap_content"
  42. android:layout_height="wrap_content"
  43. android:ems="10"
  44. android:textSize="25sp" />
  45. </TableRow>
  46.  
  47. <TableRow
  48. android:id="@+id/tableRow2"
  49. android:layout_width="match_parent"
  50. android:layout_height="wrap_content" >
  51.  
  52. <TextView
  53. android:id="@+id/textView2"
  54. android:layout_width="wrap_content"
  55. android:layout_height="wrap_content"
  56. android:text="password" />
  57.  
  58. <EditText
  59. android:id="@+id/editText2"
  60. android:layout_width="wrap_content"
  61. android:layout_height="wrap_content"
  62. android:ems="10"
  63. android:textSize="25sp" />
  64. </TableRow>
  65.  
  66. <TableRow
  67. android:id="@+id/tableRow3"
  68. android:layout_width="match_parent"
  69. android:layout_height="wrap_content" >
  70.  
  71. <Button
  72. android:id="@+id/button1"
  73. android:layout_width="wrap_content"
  74. android:layout_height="wrap_content"
  75. android:layout_weight="1"
  76. android:text="取消" />
  77.  
  78. <Button
  79. android:id="@+id/button2"
  80. android:layout_width="wrap_content"
  81. android:layout_height="wrap_content"
  82. android:layout_weight="1"
  83. android:text="登录" />
  84. </TableRow>
  85. </LinearLayout>
  86.  
  87. <LinearLayout
  88. android:id="@+id/image"
  89. android:layout_width="match_parent"
  90. android:layout_height="match_parent"
  91. android:orientation="vertical" >
  92.  
  93. <ImageView
  94. android:layout_width="wrap_content"
  95. android:layout_height="wrap_content"
  96. android:src="@drawable/a2" />
  97. </LinearLayout>
  98.  
  99. <LinearLayout
  100. android:id="@+id/timer"
  101. android:layout_width="match_parent"
  102. android:layout_height="match_parent"
  103. android:orientation="vertical" >
  104.  
  105. <TimePicker
  106. android:layout_width="match_parent"
  107. android:layout_height="wrap_content" />
  108. </LinearLayout>
  109. <LinearLayout
  110. android:id="@+id/timer1"
  111. android:layout_width="match_parent"
  112. android:layout_height="match_parent"
  113. android:orientation="vertical" >
  114.  
  115. <TimePicker
  116. android:layout_width="match_parent"
  117. android:layout_height="wrap_content" />
  118. </LinearLayout>
  119. <LinearLayout
  120. android:id="@+id/timer2"
  121. android:layout_width="match_parent"
  122. android:layout_height="match_parent"
  123. android:orientation="vertical" >
  124.  
  125. <TimePicker
  126. android:layout_width="match_parent"
  127. android:layout_height="wrap_content" />
  128. </LinearLayout>
  129. </FrameLayout>
  130. </RelativeLayout >
  131. </TabHost>

JAVA文件配置


  1. package com.example.tabhost;
  2.  
  3. import android.os.Bundle;
  4. import android.app.Activity;
  5. import android.widget.TabHost;
  6. import android.widget.TabHost.TabSpec;
  7.  
  8. public class MainActivity extends Activity { // 直接继承Activity
  9. private TabHost myTabHost; // 定义TabHost
  10. private int[] layRes = { R.id.login, R.id.image
  11. , R.id.timer, R.id.timer1, R.id.timer2 }; // 定义内嵌布局管理器ID
  12. private int images[]={R.drawable.cart,R.drawable.cloud,R.drawable.comment,R.drawable.gear,R.drawable.joystick};//标题图片数据
  13.  
  14. @Override
  15. public void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. super.setContentView(R.layout.activity_main) ; // 调用默认布局管理器
  18. this.myTabHost = (TabHost) super.findViewById(R.id.tabhost); // 取得TabHost对象
  19. this.myTabHost.setup() ; // 建立TabHost对象
  20. for (int x = 0; x < this.layRes.length; x++) { // 循环取出全部布局标记
  21. TabSpec myTab = myTabHost.newTabSpec("tab" + x); // 定义TabSpec
  22. myTab.setIndicator(null,getResources().getDrawable(images[x])) ; // 设置标签文字
  23. myTab.setContent(this.layRes[x]) ; // 设置显示的组件
  24. this.myTabHost.addTab(myTab) ; // 添加标签
  25. }
  26. this.myTabHost.setCurrentTab(0) ; // 设置開始索引
  27. }
  28. }






使用TabHost组件设置Tab切换与intent的结合在开发中较经常使用到,是app开发框架的基础

下节预报:
Menu菜单


从零開始学android&lt;TabHost标签组件.二十九.&gt;的更多相关文章

  1. 从零開始学android&lt;SeekBar滑动组件.二十二.&gt;

    拖动条能够由用户自己进行手工的调节,比如:当用户须要调整播放器音量或者是电影的播放进度时都会使用到拖动条,SeekBar类的定义结构例如以下所看到的: java.lang.Object    ↳ an ...

  2. 从零開始学android&lt;Menu菜单组件.三十.&gt;

    在Android系统之中.菜单一共同拥有三类:选项菜单(OptionsMenu).上下文菜单(ContextMenu)和子菜单(SubMenu). 今天我们就用几个样例来分别介绍下菜单的使用 acti ...

  3. 从零開始学android&lt;Bitmap图形组件.四十七.&gt;

    android.graphics.Bitmap(位图)是Android手机中专门提供的用于操作图片资源的操作类,使用此类能够直接从资源文件之中进行图片资源的读取.而且对这些图片进行一些简单的改动. 经 ...

  4. 从零開始学android&lt;数据存储(1)SharedPreferences属性文件.三十五.&gt;

    在android中有五种保存数据的方法.各自是: Shared Preferences Store private primitive data in key-value pairs. 相应属性的键值 ...

  5. 第13章、布局Layouts之RelativeLayout相对布局(从零開始学Android)

    RelativeLayout相对布局 RelativeLayout是一种相对布局,控件的位置是依照相对位置来计算的,后一个控件在什么位置依赖于前一个控件的基本位置,是布局最经常使用,也是最灵活的一种布 ...

  6. 从零開始学android&lt;mediaplayer自带播放器(视频播放).四十九.&gt;

    MediaPlayer除了能够对音频播放之外,也能够对视频进行播放,可是假设要播放视频仅仅依靠MediaPlayer还是不够的.还须要编写一个能够用于视频显示的空间,而这块显示空间要求能够高速的进行G ...

  7. 从零開始学android&lt;ImageSwitcher图片切换组件.二十六.&gt;

    ImageSwitcher组件的主要功能是完毕图片的切换显示,比如用户在进行图片浏览的时候.能够通过button点击一张张的切换显示的图片,并且使用ImageSwitcher组件在每次切换的时候也能够 ...

  8. 从零開始学android&lt;RelativeLayout相对布局.十六.&gt;

    相对布局管理器指的是參考某一其它控件进行摆放,能够通过控制,将组件摆放在一个指定參考组件的上.下.左.右等位置,这些能够直接通过各个组件提供的属性完毕. 以下介绍一下各个方法的基本使用 No. 属性名 ...

  9. 从零開始学android&lt;使用嵌套布局实现计算器界面.十七.&gt;

    所谓的嵌套布局就是在一个文件里嵌套多个布局文件 <span style="font-size:18px;"> <LinearLayout android:layo ...

随机推荐

  1. Android 推断程序在手机中是否是活动状态或者正在执行状态

    沈阳斌子在今天项目需求上碰到个这种问题,在Service中须要推断当前的程序是否是活动状态,换句话说也就是说后台跑的服务中有业务需求检測当前程序是否是该服务的程序 这样好让点击推送通知时跳转到不同的页 ...

  2. 重构版机房收费系统之分层、接口、数据库连接、反射+工厂(vb.net)

    分层 分层是为了减少层与层之间的依赖,添加程序的可读性,让整个系统结构清晰明白.还可大大减少维护成本,可是分层也有一定的缺点,有些能够直接訪问数据库的层,却要通过负责訪问数据库的层进行訪问.这样,在訪 ...

  3. 51nod-1363: 最小公倍数之和

    [传送门:51nod-1363] 简要题意: 给出一个数n,求出1到n的数与n的最小公倍数的和 多组数据 题解: 理所当然推柿子 原题相当于求$\sum_{i=1}^{n}\frac{i*n}{gcd ...

  4. 求区间连续不超过K段的最大和--线段树+大量代码

    题目描述: 这是一道数据结构题. 我们拥有一个长度为n的数组a[i]. 我们有m次操作.操作有两种类型: 0 i val:表示我们要把a[i]修改为val; 1 l r k:表示我们要求出区间[l,r ...

  5. php面向对象之__isset和__unset

    php面向对象之__isset和__unset 一.简介 __isset和__unset都是对不可访问属性的操作,前者是检验的时候自动调用,后者是销毁的时候自动调用. 比如说在类外访问private的 ...

  6. HD-ACM算法专攻系列(14)——find your present (2)

    问题描述: 源码: #include"iostream" #include"algorithm" using namespace std; bool cmp(i ...

  7. Core Java(五)

    类和对象&方法 ——类的定义 现实世界的事物 属性:人的身高,体重等 行为:人可以学习,吃饭等 Java中用class描述事物也是如此 成员变量:就是事物的属性 成员方法:就是事物的行为    ...

  8. 卡片式大学综合英语词汇(Windows Phone 8.1 RT app)

    简易卡片式记单词app.词库是原滋原味的大学综合英语词汇,包含语音,使用卡片式设计.离线词库,随时随地记单词. 商店:http://www.windowsphone.com/zh-cn/store/a ...

  9. Arduino扫盲(持续添加中)

    1.Arduino火的很,很大一点在于,他基本透明掉了硬件电子部分,只剩下软件部分,通过把电子部分包装成黑箱,使得大量IT人士,普通人,甚至小学生也能玩的来. 2 .Arduino是一个电子原型开发平 ...

  10. 第五章 Python之装饰器

    函数对象 函数是第一类对象:即函数可以当作数据传递 #可以被引用,可以被当作参数传递,返回值可以是函数,可以当作容器类型的元素 #引用 def func(x,y): print(x,y) f=func ...