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" > <LinearLayout android:layout_marginTop="50dp" android:id="@+id/login" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TableRow android:id="@+id/tableRow1" android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="账号" /> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:textSize="25sp" /> </TableRow> <TableRow android:id="@+id/tableRow2" android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="password" /> <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:textSize="25sp" /> </TableRow> <TableRow android:id="@+id/tableRow3" android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="取消" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="登录" /> </TableRow> </LinearLayout> <LinearLayout android:layout_marginTop="50dp" android:id="@+id/image" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/a2" /> </LinearLayout> <LinearLayout android:layout_marginTop="50dp" android:id="@+id/timer" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TimePicker android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:layout_marginTop="50dp" android:id="@+id/timer1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TimePicker android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:layout_marginTop="50dp" android:id="@+id/timer2" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TimePicker android: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="password" /><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菜单

从零開始学androidlt;TabHost标签组件.二十九.gt;相关推荐

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

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

  2. 从零开始学androidTabHost标签组件.二十九.

    TabHost主要特点是可以在一个窗口中显示多组标签栏的内容,在Android系统之中每个标签栏就称为一个Tab,而包含这多个标签栏的容器就将其称为TabHost,TabHost类的继承结构如下所示: ...

  3. 小甲鱼python笔记_小甲鱼《零基础学习Python》课后笔记(二十九):文件——一个任务...

    动动手 0.编写一个程序,接受用户的输入并保存为新的文件,程序实现如图: 代码如下:f_name = input('请输入文件名:') f = open(f_name, 'wt') print(&qu ...

  4. # 从零開始搭建Hadoop2.7.1的分布式集群

    Hadoop 2.7.1 (2015-7-6更新),Hadoop的环境配置不是特别的复杂,可是确实有非常多细节须要注意.不然会造成很多配置错误的情况.尽量保证一次配置正确防止重复改动. 网上教程有非常 ...

  5. 從零開始學 ReactJS:最完整的 React 生態系入門教程

    转自:https://github.com/kdchang/reactjs101 從零開始學 ReactJS(ReactJS 101)是一本希望讓初學者一看就懂的 ReactJS 中文入門教學書,由淺 ...

  6. 【零基础学Java】—多线程(四十九)

    [零基础学Java]-多线程(四十九) 一.并发与并行 并发:指两个或多个事件在同一时间段内发生 并行:指两个或多个事件在同一时刻发生(同时发生) 二.进程和线程

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

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

  8. 【零基础学Java】—重写(二十)

    [零基础学Java]-重写(二十) 一.重写和重载 重写(Override):在继承关系当中,方法的名称一样,参数列表一样.重写又称为方法的覆盖.覆写. 重载(Overload):方法的名称一样,参数 ...

  9. [Golang] 从零開始写Socket Server(3): 对长、短连接的处理策略(模拟心跳)

    通过前两章,我们成功是写出了一套凑合能用的Server和Client,并在二者之间实现了通过协议交流.这么一来,一个简易的socket通讯框架已经初具雏形了,那么我们接下来做的.就是想办法让这个框架更 ...

  10. Spring 从零開始-05

    最终能到Spring的AOP编程了,AOP的概念特别的多.所以须要你在開始之前有点了解,然后通过代码慢慢学习! - 切面(Aspect):一个关注点的模块化,这个关注点实现可能另外横切多个对象.事务管 ...

最新文章

  1. Google AI骗过了Google,工程师竟无计可施?
  2. python有多态特性吗_Python:多态、协议和鸭子类型
  3. MySQL 高级 循环获取游标
  4. 数据科学 IPython 笔记本 8.3 Matplotlib 可视化
  5. SpringBoot - 多Profile使用与切换
  6. 圣思园java se培训总结(58-)(java1.5新特性,可变参数,包装类)
  7. 今日头条成锤子“接盘侠”?“是真的!”
  8. slz-servlet的引入
  9. 洛谷 P2383 狗哥玩木棒
  10. 【UE】关于UE的一个真实案例
  11. 苹果服务器消息转发,好消息!微信语音也可以转发啦!不好的消息!目前苹果还不行!...
  12. latex数学公式转换器
  13. java B2B2C Springcloud电子商城系统- Gateway初体验
  14. 五年引用量最高的 10 大论文:Adam 登顶,AlphaGo、Transfromer 上榜
  15. Linux软件包企业实战案例
  16. 互联网产品类型与产品工具
  17. linux下安装mysql数据库[yum install版]
  18. #千锋逆战班,拼搏永向前#
  19. java大数类阶乘_Java中的大数阶乘
  20. 1.PS新手入门教程

热门文章

  1. cocos2D icon
  2. 项目成本管理---控制成本
  3. 简单的汉字和十六进制转换
  4. 半边数据结构(The_Half-Edge_Data_Structure)
  5. Burp Suite详细使用教程-Intruder模块详3
  6. UNIGUI中如果获得Session情况
  7. 在 Delphi 下使用 DirectSound (1): 枚举播放设备
  8. vue base64加密对象_想加密JavaScript怎么办,试试这款加密库!
  9. 大数据开发之路:hive篇,你看了吗?
  10. js my_first