TabHost是整个Tab的容器,包含TabWidget和FrameLayout两个部分,TabWidget是每个Tab的表情,FrameLayout是Tab内容。

实现方式有两种:

1、继承TabActivity

2、继承Activity类

>>两个方法实例下载

方法一:继承TabActivity

从TabActivity中用getTabHost()方法获取TabHost,然后设置标签内容

布局:

1、TabHost    必须设置android:id为@android:id/tabhost
2、TabWidget   必须设置android:id为@android:id/tabs
3、FrameLayout   必须设置android:id为@android:id/tabcontent

否则将出现类似报错:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/tabhost"

> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
> <TabWidget
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@android:id/tabs"
></TabWidget> <FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@android:id/tabcontent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/widget_layout_red"
android:background="#ff0000"
android:orientation="vertical"
></LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/widget_layout_yellow"
android:background="#FCD209"
android:orientation="vertical"
></LinearLayout> </FrameLayout>
</LinearLayout>
</TabHost>

继承TabActivity

public class MainActivity extends TabActivity {
private TabHost tabhost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo); //从TabActivity上面获取放置Tab的TabHost
tabhost = getTabHost(); tabhost.addTab(tabhost
//创建新标签one
.newTabSpec("one")
//设置标签标题
.setIndicator("红色")
//设置该标签的布局内容
.setContent(R.id.widget_layout_red));
tabhost.addTab(tabhost.newTabSpec("two").setIndicator("黄色").setContent(R.id.widget_layout_yellow));
} }

其中创建标签的方法:

 tabhost.addTab(tabhost
.newTabSpec("one")
.setIndicator("红色")
.setContent(R.id.widget_layout_red));

也可以拆分写成:

 TabHost.TabSpec tab1 = tabhost.newTabSpec("one");
tab1.setIndicator("红色");
tab1.setContent(R.id.widget_layout_red);
tabhost.addTab(tab1);

预览:

点击"黄色"标签

点击"红色"标签

 方法二:继承Activity类

布局:

1、TabHost    可自定义id
2、TabWidget   必须设置android:id为@android:id/tabs
3、FrameLayout   必须设置android:id为@android:id/tabcontent

public class MainActivity extends Activity{
private TabHost tabhost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo); //得到TabHost对象实例
tabhost =(TabHost) findViewById(R.id.mytab);
//调用 TabHost.setup()
tabhost.setup();
//创建Tab标签
tabhost.addTab(tabhost.newTabSpec("one").setIndicator("红色").setContent(R.id.widget_layout_red));
tabhost.addTab(tabhost.newTabSpec("two").setIndicator("黄色").setContent(R.id.widget_layout_yellow)); } }

注意的是:

在使用TabHost切换activity时出现Did you forget to call 'public void setup..

改用第一种方法吧

其他实例:

Android:布局实例之模仿微信Tab

Android:TabHost实现Tab切换的更多相关文章

  1. Android之TabHost实现Tab切换

    TabHost是整个Tab的容器,包含TabWidget和FrameLayout两个部分,TabWidget是每个Tab的表情,FrameLayout是Tab内容. 实现方式有两种: 1.继承TabA ...

  2. Android - TabHost 与 Fragment 制作页面切换效果

    Android - TabHost 与 Fragment 制作页面切换效果 Android API 19 , API 23 三个标签页置于顶端 效果图: 在文件BoardTabHost.java中定义 ...

  3. Android典型界面设计——FragmentTabHost+Fragment实现底部tab切换

    一.问题描述 在上次博文中,我们使用RadioGroup+ViewPage+Fragmen实现了顶部滑动导航(查看文章:http://www.cnblogs.com/jerehedu/p/460759 ...

  4. Android Studio精彩案例(二)《仿微信动态点击底部tab切换Fragment》

    转载本专栏文章,请注明出处,尊重原创 .文章博客地址:道龙的博客 现在很多的App要么顶部带有tab,要么就底部带有tab.用户通过点击tab从而切换不同的页面(大部分情况时去切换fragment). ...

  5. Android典型界面设计(3)——访网易新闻实现双导航tab切换

    一.问题描述 双导航tab切换(底部区块+区域内头部导航),实现方案底部区域使用FragmentTabHost+Fragment, 区域内头部导航使用ViewPager+Fragment,可在之前博客 ...

  6. Android典型界面设计-访网易新闻实现双导航tab切换

    一.问题描述 双导航tab切换(底部区块+区域内头部导航),实现方案底部区域使用FragmentTabHost+Fragment, 区域内头部导航使用ViewPager+Fragment,可在之前博客 ...

  7. Android UI 之 Tab类型界面总结

    Android 程序中实现Tab类型界面很常见,本人在做项目的时候也经常用到,所以想在这里总结一下,实现tab类型界面的几种方式,供大家参考.如有不对之处,欢迎大家指正! 一.TabActivity ...

  8. Android ActionBar通过Tab进行不同的Fragment之间的交换

    ActionBar的使用常见于4.0系统,其Tab的使用挺广泛的. 在ActionBar中添加标签(Tabs),每个标签对应的是一个Fragment,点击不同的Tab时,就会切换到对应的Fragmen ...

  9. Android——TabHost(标签容器)相关知识总结贴

    android 2.3 r1 中文 api (58) —— TabHost http://www.apkbus.com/android-18911-1-1.html   android中文api (5 ...

随机推荐

  1. duilib底层机制剖析:窗体类与窗体句柄的关联

    转载请说明原出处,谢谢~~ 看到群里朋友有人讨论WTL中的thunk技术,让我联想到了duilib的类似技术.这些技术都是为了解决c++封装的窗体类与窗体句柄的关联问题. 这里是三篇关于thunk技术 ...

  2. opencv行人检测里遇到的setSVMDetector()问题

    参考了博客http://blog.csdn.net/carson2005/article/details/7841443 后,自己动手后发现了一些问题,博客里提到的一些问题没有解决 ,是关于为什么图像 ...

  3. CSS基础知识——选择器

    选择器 元素选择器# 文档元素为最基本的选择器 例子:div{属性:值}; 选择器分组 例子:h2,p{属性:值}; 表示符合这两种规则的元素设置相同的属性值 通配选择器 表示所有元素 类选择器 应用 ...

  4. Python面向对象2

    方法 公共方法 私有方法 类方法 静态方法 #!usr/bin/python #coding:utf8 class Milo(): name = 'csvt' def fun1(self): prin ...

  5. POJ 1004 解题报告

    1.题目描述: http://poj.org/problem?id=1004 2.解题过程 这个题目咋一看很简单,虽然最终要解出来的确也不难,但是还是稍微有些小把戏在里面,其中最大的把戏就是float ...

  6. Components of the Impala Server

    Components of the Impala Server The Impala server is a distributed, massively parallel processing (M ...

  7. hadoop多次格式化后,导致datanode启动不了,怎么办?(伪分布式)

    根据当初 hadoop 安装目录下 conf 目录的 core-site.xml 的设置,找到该目录:   进入该目录 在 data 和 name 文件夹下均有  current 文件夹 ,和 cur ...

  8. linux - 怎么自动填写有交互的shell脚本 - SegmentFault

    linux - 怎么自动填写有交互的shell脚本 - SegmentFault TCL/Expect交互式自动化测试概要 - - ITeye技术网站 expect是一种基于TCL,能与交互式程序进行 ...

  9. homework-04

    1.准备工作 本次结对编程我们对项目本身就行了分工,既然是测试来驱动开发,那么我们就把本次工作分成了测试与生成两个部分,小明同学负责生成测试数据,而我写测试程序检测测试结果是否正确,相对来说还是小明同 ...

  10. css知识汇总

    <style type="text/css"> table{ border-collapse:collapse; } table, td, th{ border:1px ...