选项卡(TabHost)的功能与用法
TabHost是一种非常实用的组件,TabHost可以很方便地在窗口上放置多个便签页,每个标签页相当于获得了一个与外部容器相同大小的组件摆法区域。通过这种方式,就可以在一个容器里放置更多组件,例如手机系统都会在同一个窗口定义多个便签来显示通话记录,包括“未接电话”、“已接电话”、“呼出电话”等。
与TabHost结合使用的还有如下组件。
- TabWidget:代表选项卡的标签条。
- TabSpec:代表选项卡的一个Tab页面。
TabHost仅仅是一个简单的容器,它提供了如下两个方法来创建、添加选项卡。
- newTabSpec(String tag):创建选项卡。
- addTab(TabHost.TabSpec tabSpec):添加选项卡。
使用TabHost的一般步骤如下。
- 在界面布局中定义TabHost组件,并为该组件定义该选项卡的内容。
- Activity应该继承TabActivity。
- 调用TabActivity的getTabHost()方法获取TabHost对象。
- 通过TabHost对象的方法来创建、添加选项卡。
除此之外,TabHost还提供了一些方法获取当前选项卡,获取当前View的方法,具体可参考API文档。如果程序需要监控TabHost里当前标签页的改变,可以为它设置TabHost.OnTabChangeListener监听器。
下面通过一个简单的实例来示范选项卡的用法。
实例:通话记录页面
下面的示例程序使用TabHost定义一个标签容器,并使用了三个LinearLayout来定义标签页(实际上可以使用任何View组件来定义标签页)。该程序的界面布局文件如下。
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 定义一个标签页的内容 -->
<LinearLayout android:id="@+id/tab01"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/tx01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="女儿国国王 - 2013/08/31"/>
<TextView android:id="@+id/tx02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="东海龙女 - 2013/08/31"/>
</LinearLayout>
<!-- 定义第二个标签页的内容 -->
<LinearLayout android:id="@+id/tab02"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="@+id/tx03"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="火龙王 - 13969004275"
/>
<TextView android:id="@+id/tx04"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="牛魔王- 13791030073"
/>
</LinearLayout>
<!-- 定义第三个标签的内容 -->
<LinearLayout android:id="@+id/tab03"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:textSize="11pt">
<TextView android:id="@+id/tx05"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="齐天大圣 - 13969004275"/>
<TextView android:id="@+id/tx06"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="孙悟空- 13791030073"/> </LinearLayout>
</FrameLayout> </LinearLayout> </TabHost>
请注意上面的布局文件中粗体字代码部分,从上面布局文件中可以发现,TabHost容器内部需要组合两个组件:TabWidget和FrameLayout,其中TabWidget定义选项卡的标题条;FrameLayout则用于“层叠”组合多个选项卡页面。不仅如此,上面的布局文件中这三个组件的ID也有改变。
- TabHost的ID应该为@android:id/tabhos。
- TabWidget的ID应该为@android:id/tabs。
- FrameLayout的ID应该为@android:id/tabcontent。
上面这三个ID并不是开发者自己定义的,而是引用了Android系统已有的ID。
接下来主程序即可加载该布局资源,并将布局文件中的三个Tab页面添加到该TabHost容器中。该Activity代码如下。
package org.crazyit.helloworld; import android.os.Bundle;
import android.app.Activity;
import android.app.TabActivity;
import android.view.Menu;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec; public class TabHostTest extends TabActivity { @SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_host_test);
//获取该Activity里面的TabHost组价
@SuppressWarnings("deprecation")
TabHost tabHost=getTabHost();
//创建第一个Tab页
TabSpec tab1=tabHost.newTabSpec("tab1")
.setIndicator("已接电话")//设置标题
.setContent(R.id.tab01);//设置内容
//添加第一个标签页
tabHost.addTab(tab1); TabSpec tab2=tabHost.newTabSpec("tab2")
//在标签标题上放置图标
.setIndicator("呼出电话",getResources().getDrawable(R.drawable.ic_action_search))
.setContent(R.id.tab02);
//添加第二个标签
tabHost.addTab(tab2);
TabSpec tab3=tabHost.newTabSpec("tab3")
.setIndicator("未接电话").setContent(R.id.tab03);
//添加第三个标签页
tabHost.addTab(tab3);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.tab_host_test, menu);
return true;
} }
上面的程序中粗体字代码就是为TabHost创建、并添加Tab页面的代码。上面的程序中一共添加三个标签页,因此类似粗体字的代码一共写了三次。其中第二个标签的标题上还添加了一个图片。
运行上面的程序将看到如下效果:

上面的程序调用了TabHost.TabSpec对象的setContent(int viewID)方法来设置标签页内容;除此之外还可调用setContent(Intent intent)方法来设置标签页内容,Intent还可用于启动其他Activity——这意味着TabHost.TabSpec可直接装载另一个Activity。
选项卡(TabHost)的功能与用法的更多相关文章
- Android使用Fragment来实现TabHost的功能(解决切换Fragment状态不保存)以及各个Fragment之间的通信
以下内容为原创,转载请注明:http://www.cnblogs.com/tiantianbyconan/p/3360938.html 如新浪微博下面的标签切换功能,我以前也写过一篇博文(http:/ ...
- Android 自学之画廊视图(Gallery)功能和用法
Gallery与之前讲的Spinner有共同的父类:AbsSpinner,表明Gallery和Spinner都是一个列表框.他们之间的区别在于Spinner显示的是一个垂直的列表框,而Gallery显 ...
- Android 自学之选项卡TabHost
选项卡(TabHost)是一种非常实用的组件,TabHost可以很方便地在窗口上放置多个标签页,每个标签页相当于获得了一个与外部容器相同大小的组建摆放区域.通过这种方式,就可以在一个容器中放置更多组件 ...
- 搜索框(SearchView)的功能与用法
SearchView是搜索框组件,它可以让用户在文本框内输入汉字,并允许通过监听器监控用户输入,当用户用户输入完成后提交搜索按钮时,也通过监听器执行实际的搜索. 使用SearchView时可以使用如下 ...
- 数值选择器(NumberPicker)的功能与用法
数值选择器用于让用户输入数值,用户既可以通过键盘输入数值,也可以通过拖动来选择数值.使用该组件常用如下三个方法. setMinValue(int minVal):设置该组件支持的最小值. setMax ...
- 日历视图(CalendarView)组件的功能和用法
日历视图(CalendarView)可用于显示和选择日期,用户既可选择一个日期,也可通过触摸来滚动日历.如果希望监控该组件的日历改变,可调用CalendarView的setOnDateChangeLi ...
- 星级评分条(RatingBar)的功能和用法
星级评分条与拖动条有相同的父类:AbsSeekBar,因此它们十分相似.实际上星级评分条与拖动条的用法.功能都十分接近:它们都是允许用户通过拖动条来改变进度.RatingBar与SeekBar最大区别 ...
- 拖动条(SeekBar)的功能和用法
拖动条和进度条非常相似,只是进度条采用颜色填充来表明进度完成的程序,而拖动条则通过滑块的位置来标识数值——而且拖动条允许用户拖动滑块来改变值,因而拖动条通常用于对系统的某种数值进行调节,比如调节音量等 ...
- StackView的功能和用法
StackView也是AdapterViewAnimator的子类,它也用于显示Adapter提供的系列View.SackView将会以“堆叠(Stack)”方式来显示多个列表项. 为了控制Stack ...
- MySQL常用存储引擎功能与用法详解
本文实例讲述了MySQL常用存储引擎功能与用法. MySQL存储引擎主要有两大类: 1. 事务安全表:InnoDB.BDB. 2. 非事务安全表:MyISAM.MEMORY.MERGE.EXAMPLE ...
随机推荐
- vim的复制粘贴小结(转)
原文地址:http://lsong17.spaces.live.com/blog/cns!556C21919D77FB59!603.entry 内容: 用vim这么久 了,始终也不知道怎么在vim中使 ...
- dwr.xml 配置
dwr.xml 是你用来配置 DWR 的文件,默认是将其放入 WEB-INF 文件夹. 创建一个 dwr.xml 文件dwr.xml 有如下的结构: <?xml version="1. ...
- Python3基础 函数名.__doc__显示一个函数的单行与多行函数文档
镇场诗: 诚听如来语,顿舍世间名与利.愿做地藏徒,广演是经阎浮提. 愿尽吾所学,成就一良心博客.愿诸后来人,重现智慧清净体.-------------------------------------- ...
- C. Bear and Colors
C. Bear and Colors time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Android音频系统之音频框架
1.1 音频框架 转载请注明,From LXS, http://blog.csdn.net/uiop78uiop78/article/details/8796492 Android的音频系统在很长一段 ...
- 提示:ArcGIS version not specified. You must call RuntimeManager.Bind before creating any ArcGIS components.错误
ArcGIS10,然后就使用VS创建一个简单的AE应用程序,然后拖放一个toolbar.LicenseControl以及MapControl控件. 接着编译应用程序,编译成功. 然后单击F5运行程序, ...
- 红黑树(C#)
红黑树C#算法. 在线javascript演示地址:http://sandbox.runjs.cn/show/2nngvn8w using System; using System.Collectio ...
- 理解MySQL——索引与优化(转)
转自:http://www.cnblogs.com/hustcat/archive/2009/10/28/1591648.html 写在前面:索引对查询的速度有着至关重要的影响,理解索引也是进行数据库 ...
- iBATIS的多对多 数据库设计及实现
iBATIS的多对多映射配置方法和多对一映射配置方法差不多,不同的是,多对多映射,数据库设计上需要一个记录两个类关系的中间表,本文以学生-老师为例,在iBATIS的sqlmap中配置多对多关系. iB ...
- MySQL查询in操作 查询结果按in集合顺序显示_Mysql_脚本之家
body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...