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中显示不同的内容
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><LinearLayoutandroid:layout_marginTop="50dp"android:id="@+id/login"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TableRowandroid:id="@+id/tableRow1"android:layout_width="match_parent"android:layout_height="wrap_content" ><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="账号" /><EditTextandroid:id="@+id/editText1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:ems="10"android:textSize="25sp" /></TableRow><TableRowandroid:id="@+id/tableRow2"android:layout_width="match_parent"android:layout_height="wrap_content" ><TextViewandroid:id="@+id/textView2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="密码" /><EditTextandroid:id="@+id/editText2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:ems="10"android:textSize="25sp" /></TableRow><TableRowandroid:id="@+id/tableRow3"android:layout_width="match_parent"android:layout_height="wrap_content" ><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:text="取消" /><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:text="登录" /></TableRow></LinearLayout><LinearLayoutandroid:layout_marginTop="50dp"android:id="@+id/image"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/a2" /></LinearLayout><LinearLayoutandroid:layout_marginTop="50dp"android:id="@+id/timer"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TimePickerandroid:layout_width="match_parent"android:layout_height="wrap_content" /></LinearLayout><LinearLayoutandroid:layout_marginTop="50dp"android:id="@+id/timer1"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TimePickerandroid:layout_width="match_parent"android:layout_height="wrap_content" /></LinearLayout><LinearLayoutandroid:layout_marginTop="50dp"android:id="@+id/timer2"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TimePickerandroid:layout_width="match_parent"android:layout_height="wrap_content" /></LinearLayout>
</LinearLayout>
JAVA文件设置
package com.example.tabhost;import android.app.TabActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;public class MainActivity extends TabActivity {private TabHost tabHost;//初始化TabHost组件private int reslayout[] = { R.id.login, R.id.image, R.id.timer , R.id.timer1,R.id.timer2};//设置对应的额xml文件private int images[]={R.drawable.cart,R.drawable.cloud,R.drawable.comment,R.drawable.gear,R.drawable.joystick};//设置显示的标题文件@SuppressWarnings("deprecation")@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);this.tabHost = super.getTabHost();//实例化TabHost组件// 取得LayoutInflater对象LayoutInflater.from(this).inflate(R.layout.linearlayout,//制定布局this.tabHost.getTabContentView(),//制定标签增加的容器 true);for (int i = 0; i < reslayout.length; i++) {TabSpec myTab = tabHost.newTabSpec("tab" + i);// 定义TabSpecmyTab.setIndicator(null,getResources().getDrawable(images[i])) ;       // 设置标签myTab.setContent(this.reslayout[i]) ;        // 设置显示的组件this.tabHost.addTab(myTab) ;}}}

效果图如下


使用配置文件设置。
这种方法较为常见,可以讲TabHost置于底部
但是布局文件较为复杂,大家可以参照例子进行具体的学习
XML文件
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/tabhost" android:orientation="vertical"android:layout_width="fill_parent" android:layout_height="fill_parent"><RelativeLayout 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"android:layout_alignParentBottom="true" /><FrameLayout android:id="@android:id/tabcontent"android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayoutandroid:id="@+id/login"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TableRowandroid:id="@+id/tableRow1"android:layout_width="match_parent"android:layout_height="wrap_content" ><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="账号" /><EditTextandroid:id="@+id/editText1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:ems="10"android:textSize="25sp" /></TableRow><TableRowandroid:id="@+id/tableRow2"android:layout_width="match_parent"android:layout_height="wrap_content" ><TextViewandroid:id="@+id/textView2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="密码" /><EditTextandroid:id="@+id/editText2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:ems="10"android:textSize="25sp" /></TableRow><TableRowandroid:id="@+id/tableRow3"android:layout_width="match_parent"android:layout_height="wrap_content" ><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:text="取消" /><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:text="登录" /></TableRow></LinearLayout><LinearLayoutandroid:id="@+id/image"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/a2" /></LinearLayout><LinearLayoutandroid:id="@+id/timer"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TimePickerandroid:layout_width="match_parent"android:layout_height="wrap_content" /></LinearLayout><LinearLayoutandroid:id="@+id/timer1"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TimePickerandroid:layout_width="match_parent"android:layout_height="wrap_content" /></LinearLayout><LinearLayoutandroid:id="@+id/timer2"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TimePickerandroid:layout_width="match_parent"android:layout_height="wrap_content" /></LinearLayout></FrameLayout></RelativeLayout >
</TabHost>

JAVA文件配置

package com.example.tabhost;import android.os.Bundle;
import android.app.Activity;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;public class MainActivity extends Activity {          // 直接继承Activityprivate TabHost myTabHost;                           // 定义TabHostprivate int[] layRes = { R.id.login, R.id.image , R.id.timer, R.id.timer1, R.id.timer2 };                          // 定义内嵌布局管理器IDprivate int images[]={R.drawable.cart,R.drawable.cloud,R.drawable.comment,R.drawable.gear,R.drawable.joystick};//标题图片数据@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);super.setContentView(R.layout.activity_main) ;          // 调用默认布局管理器this.myTabHost = (TabHost) super.findViewById(R.id.tabhost); // 取得TabHost对象this.myTabHost.setup() ;                        // 建立TabHost对象for (int x = 0; x < this.layRes.length; x++) {  // 循环取出所有布局标记TabSpec myTab = myTabHost.newTabSpec("tab" + x);   // 定义TabSpecmyTab.setIndicator(null,getResources().getDrawable(images[x])) ;            // 设置标签文字myTab.setContent(this.layRes[x]) ;         // 设置显示的组件this.myTabHost.addTab(myTab) ;                // 增加标签}this.myTabHost.setCurrentTab(0) ;               // 设置开始索引}
}

使用TabHost组件设置Tab切换与intent的结合在开发中较常用到,是app开发框架的基础
下节预报:
Menu菜单

从零开始学androidTabHost标签组件.二十九.相关推荐

  1. 【零基础学Java】—final关键字与四种用法(二十九)

    [零基础学Java]-final关键字与四种用法(二十九) 一.final关键字 final关键字代表最终.不可改变的 常见的四种用法: 可以用来修饰一个类 可以用来修饰一个方法 可以用来修饰一个局部 ...

  2. 微信小程序从入坑到放弃二十九:一个小场景搞懂冒泡事件bindtap和catchtap的区别

    摘要: 在微信小程序中,bindtap事件会产生冒泡,若不加以拦截,会一直冒泡到顶端.在某些情况下,一次点击会触发若干点击事件.为了防止冒泡,使用catchtap即可解决问题.在有全屏半透明背景的弹出 ...

  3. 2021年大数据Hadoop(二十九):​​​​​​​关于YARN常用参数设置

    全网最详细的Hadoop文章系列,强烈建议收藏加关注! 后面更新文章都会列出历史文章目录,帮助大家回顾知识重点. 目录 本系列历史文章 前言 关于yarn常用参数设置 设置container分配最小内 ...

  4. JavaScript学习(二十九)—JS常用的事件

    JavaScript学习(二十九)-JS常用的事件 一.页面相关事件 onload事件:当页面中所有的标签都加载完成后厨房该事件,格式:window.onload <body><sc ...

  5. WCF技术剖析之二十九:换种不同的方式调用WCF服务[提供源代码下载]

    原文:WCF技术剖析之二十九:换种不同的方式调用WCF服务[提供源代码下载] 我们有两种典型的WCF调用方式:通过SvcUtil.exe(或者添加Web引用)导入发布的服务元数据生成服务代理相关的代码 ...

  6. 曾国藩谕纪鸿(咸丰六年九月二十九夜)- 勤俭自持,习劳习苦

    字谕纪鸿儿:         家中人来营者,多称尔举止大方,余为少慰.凡人多望子孙为大官,余不愿为大官,但愿为读书明理之君子.勤俭自持,习劳习苦,可以处乐,可以处约.此君子也.余服官二十年,不敢稍染官 ...

  7. 【Microsoft Azure 的1024种玩法】二十九.基于Azure VM快速实现网络入侵检测 (IDS) 及网络安全监视 (NSM)

    [简介] 数据包捕获是一个重要组件,可以实施网络入侵检测系统 (IDS) 并执行网络安全监视 (NSM). 我们可以借助开源 IDS 工具来处理数据包捕获,并检查潜在网络入侵和恶意活动的签名. 使用网 ...

  8. 【黑金原创教程】【FPGA那些事儿-驱动篇I 】实验二十九:LCD模块

    实验二十九:LCD模块 据说Alinx 301支持 7"TFT,好奇的朋友一定疑惑道,它们3.2"TFT以及7"TFT等两者之间究竟有何区别呢?答案很简单,前者自带控制器 ...

  9. 从零开始学数据结构和算法(二)线性表的链式存储结构

    链表 链式存储结构 定义 线性表的链式存储结构的特点是用一组任意的存储单元的存储线性表的数据元素,这组存储单元是可以连续的,也可以是不连续的. 种类 结构图 单链表 应用:MessageQueue 插 ...

最新文章

  1. SFB 项目经验-28-设置-所有用户-OWA-时区-语言-跳过-时区设置)
  2. mysql维护索引,mysql 索引优化
  3. 使用FirefoxDriver时报错Make sure firefox is installed问题
  4. Window上,启动Tomcat服务之后,关闭启动窗口,服务器也随之关闭
  5. mysql 自增语句_Mysql 自动增加设定基值的语句 | 很文博客
  6. 5分钟快速接入钉钉实现钉钉考勤
  7. php如何递归算法,详细的介绍一下PHP递归算法_PHP教程
  8. 【Git】error: RPC
  9. OSChina 周日乱弹 ——可以囤硬盘了
  10. 在 Arch Linux 玩百度 Flash 战曲游戏乱码
  11. repo同步代码_一次协作多端同步,打通看云、github互相同步(serverless实践)
  12. 如何测试判断云服务器的稳定性?
  13. 前端面试题之手写事件模型及事件代理/委托
  14. python post请求实例_Python post请求实现代码实例
  15. C4D R23 安装教程
  16. 谷歌SEO算法更新大全(2010-2021)
  17. 微信对账单 java_微信下载对账单
  18. Hello 中国,Go官网回归中国
  19. ThinkPhp5.2加减法验证码
  20. Matlab 版本对照

热门文章

  1. 【操作系统系列】进程视图与基本问题
  2. 我从建筑工程施工员转行成了数据分析师| 求职分享
  3. 菜鸟写C语言———石头剪子布
  4. 缓存雪崩、缓存击穿、缓存穿透
  5. oracle纸质许可证,收不到纸质证书或者有任何问题都可以请求oracle帮助
  6. bluelake-JPress免费博客模板
  7. 一起围观春晚“打工牛”,2021中国智能智造牛起来!
  8. 310K 是多少度?
  9. 数据驱动框架及实战---第五集
  10. 面进百度,被这份阿里大能开源的“全彩版图解 HTTP 手册”折服了,要不怎么说还得是权威啊