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. 最新!国内芯片70个细分领域重要代表企业 VS 国外
  2. windows Azure
  3. 判断TImage图片的类型
  4. Android基于mAppWidget实现手绘地图(五)--如何创建地图资源
  5. mysql和mariadb对比_MySQL并发复制系列三:MySQL和MariaDB实现对比
  6. ffmpeg库音频解码示例
  7. Cisco TrustSec(理解)
  8. CSS中div覆盖另一个div
  9. 数据结构分类概述【转载】
  10. 评分模型的监控报表汇总
  11. MATLAB plot画线的颜色设定
  12. worldcloud库的使用
  13. win7系统怎么设置sql服务器,win7系统怎么安装sqlserver2000软件(图文)
  14. matlab神经网络训练精度,关于提高MATLAB神经网络精度的问题
  15. 接口(interface、implement)
  16. 游戏策划在游戏的开发过程中扮演什么角色?——游戏策划入门
  17. 增加对IE11的兼容
  18. 计算机专业毕业设计致谢,计算机毕业论文致谢范文3篇
  19. 一小时前华为鸿蒙操作系统放出了源代码
  20. 平面设计的表现手法有哪些比较常用

热门文章

  1. JS数组删除其中一个元素
  2. CPU用户态和内核态
  3. Android Jetpack组件ViewModel基本使用和原理分析
  4. 【问题解决】检索com类工厂中clsid为 10020200-E260-11CF-AE68-00AA004A34D5 的组件时失败...
  5. 快速上手Ubuntu之安装篇——安装win7,Ubuntu16.04双系统
  6. EN 14313:PEF聚乙烯泡沫CE认证
  7. ChatGPT如何进入开发者模式
  8. 焕然一新 | 寻息科技官网重磅升级
  9. CountDownTimer使用心得及总结
  10. OutLook客户端删除邮件服务器邮件的恢复一例