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")
@Override
protected 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);// 定义TabSpec
myTab.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">
<LinearLayout 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: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: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: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: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>
</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 { // 直接继承Activity
private TabHost myTabHost; // 定义TabHost
private int[] layRes = { R.id.login, R.id.image
, R.id.timer, R.id.timer1, R.id.timer2 }; // 定义内嵌布局管理器ID
private int images[]={R.drawable.cart,R.drawable.cloud,R.drawable.comment,R.drawable.gear,R.drawable.joystick};//标题图片数据 @Override
public 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); // 定义TabSpec
myTab.setIndicator(null,getResources().getDrawable(images[x])) ; // 设置标签文字
myTab.setContent(this.layRes[x]) ; // 设置显示的组件
this.myTabHost.addTab(myTab) ; // 添加标签
}
this.myTabHost.setCurrentTab(0) ; // 设置開始索引
}
}






使用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. uva 10061(数学)

    题解:题目要在b进制下输出的是一个数字阶乘后有多少个零,然后输出一共同拥有多少位.首先计算位数,log(n)/log(b) + 1就是n在b进制下有多少位,而log有个公式就是log(M×N) = l ...

  2. intel dpdk在ubuntu12.04中測试testpmd、helloworld程序

    一.測试环境 操作系统:ubuntu12.04   x86_64 dpdk版本号:1.6.0r2 虚拟机:vmware 10 网卡: Intel Corporation 82545EM Gigabit ...

  3. 2014年湖北省TI杯大学生电子设计竞赛论文格式

    2014年湖北省TI杯大学生电子设计竞赛 B题:金属物体探測定位器(本科) 2014年8月15日 文件夹 1 系统方案 1.1 XXX的论证与选择........................... ...

  4. Android recovery UI实现分析

    Android recovery模式为何物? 关于这个问题, baidu上已经有无数的答案.不理解的朋友先补习一下. 从纯技术角度来讲, recovery和android本质上是两个独立的rootfs ...

  5. Android图文混排-实现EditText图文混合插入上传

    前段时间做了一个Android会议管理系统,项目需求涉及到EditText的图文混排,如图: 在上图的"会议详情"中.须要支持文本和图片的混合插入,下图演示输入的演示样例: 当会议 ...

  6. 转:Oracle GoldenGate学习之Goldengate介绍

    转自:http://blog.sina.com.cn/s/blog_a32eff28010136d9.html 日志或归档日志获得数据的增删改变化,再将这些变化应用到目标数据库,实现源数据库与目标数据 ...

  7. caffe-ssd使用预训练模型做目标检测

    首先参考https://www.jianshu.com/p/4eaedaeafcb4 这是一个傻瓜似的目标检测样例,目前还不清楚图片怎么转换,怎么验证,后续继续跟进 模型测试(1)图片数据集上测试 p ...

  8. Android系统驱动【转】

    本文转载自:http://www.hovercool.com/en/%E6%B7%BB%E5%8A%A0%E9%A9%B1%E5%8A%A8%E6%A8%A1%E5%9D%97#a_.E5.9B.9B ...

  9. Python学习小计

    1.初学Python最好选择2.7版本,因为大部分Python书籍的示例代码是基于这个版本的 2.Python安装可以参考百度经验完成 如果在电脑上同时安装2个版本,则CMD启动时只需要: py -2 ...

  10. Projective Texture Mapping - 投影纹理

    昨天导师让写一个投影纹理,将一个相机渲染的图片的一部分投影到另外一个相机里面,目的是无缝的拼接. 投影纹理就和shadow map一样,都是将片元转换到另外一个相机/光源坐标系下,投影后找到对应的纹素 ...