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. 在eclipse中关联android源代码

    1打包源代码成jar: 1 新建一个java项目 2  import  想打包的源代码文件 3 export 这个文件 : 选择java->jar file .  这里会让你选择输出路径 2 加 ...

  2. 【cocos2d-js官方文档】二十、moduleConfig.json

    概述 该配置文件相当于v2版本号中的jsloader.js. 改造的目的是为了使得配置纯粹化,同一时候也能比較好的支持cocos-console.cocos-utils甚至是用户自己定义脚本工具. 字 ...

  3. UVA - 11762 - Race to 1 记忆化概率

    Dilu have learned a new thing about integers, which is - any positive integer greater than 1 can bed ...

  4. vue-quill-editor 禁止编辑

    每天学习一点点,知识财富涨点点 因为权限问题需要对富文本vue-quill-editor进行禁止编辑,因为也不是专业前端,处理起问题来还是只有看文档和百度,发现对这个提问不是很多,可能很多大牛都不会出 ...

  5. HD-ACM算法专攻系列(18)——Largest prime factor

    题目描述: 源码: 需要注意,若使用cin,cout输入输出,会超时. #include"iostream" #include"memory.h" #defin ...

  6. 关于 Visual Studio 中文提示的问题

    [转自 https://blog.csdn.net/iloli/article/details/51055299] [转自 http://www.cnblogs.com/hellowin/p/6383 ...

  7. Hibernate框架学习(七)——多对多关系

    一.关系表达 1.表中的表达 2.实体中的表达 3.orm元数据中的表达 在User.hbm.xml中添加: 在Role.hbm.xml中添加(与上相反): 二.操作关联属性 1.保存员工及角色 pu ...

  8. 关于app夜间模式那点事

    大半年没写过代码了 一直在忙一些其他的事情  这几天想起来看了一点  心惊肉跳的 发现好陌生  所以打算今后慢慢的拾起来  往深度和广度去发展 发现好久之前写的一个微博项目  有一个夜间模式的功能没有 ...

  9. Custom Hosting in IIS/WAS

    常常需要与宿主实例进行交互.这对于使用自托管的方式是不可或缺的.当使用IIS或WAS时,不能直接访问宿主.为了克服这个障碍,WCF提供了一个宿主工厂.在.svc文件中使用Factory标签,使用此工厂 ...

  10. Paper-[acmi 2015]Image based Static Facial Expression Recognition with Multiple Deep Network Learning

    [acmi 2015]Image based Static Facial Expression Recognition with Multiple Deep Network Learning ABST ...