【转】http://blog.csdn.net/harvic880925/article/details/17120325

前言:今天仔细研究了下TabHost,主要是为了实现微信底部导航栏的功能,最后也给出一个链接,这位老兄用TabHost基本做出来了微信导航栏。

正文

TabHost的实现分为两种,一个是不继承TabActivity,一个是继承自TabActivity;当然了选用继承自TabActivity的话就相对容易一些,下面来看看分别是怎样来实现的吧。

方法一、定义tabhost:不用继承TabActivity

1、布局文件:activity_main.xml

[html] view plain copy

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical"
  6. tools:context=".MainActivity" >
  7. <Button
  8. android:id="@+id/button1"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:text="Button" />
  12. <TabHost
  13. android:id="@+id/tabhost"
  14. android:layout_width="match_parent"
  15. android:layout_height="wrap_content">
  16. <LinearLayout
  17. android:layout_width="match_parent"
  18. android:layout_height="match_parent"
  19. android:orientation="vertical" >
  20. <TabWidget
  21. android:id="@android:id/tabs"
  22. android:layout_width="match_parent"
  23. android:layout_height="wrap_content" >
  24. </TabWidget>
  25. <FrameLayout
  26. android:id="@android:id/tabcontent"
  27. android:layout_width="match_parent"
  28. android:layout_height="match_parent" >
  29. <!-- 第一个tab的布局 -->
  30. <LinearLayout
  31. android:id="@+id/tab1"
  32. android:layout_width="match_parent"
  33. android:layout_height="match_parent" >
  34. <TextView
  35. android:id="@+id/textView1"
  36. android:layout_width="wrap_content"
  37. android:layout_height="wrap_content"
  38. android:text="林炳东" />
  39. </LinearLayout>
  40. <!-- 第二个tab的布局 -->
  41. <LinearLayout
  42. android:id="@+id/tab2"
  43. android:layout_width="match_parent"
  44. android:layout_height="match_parent" >
  45. <TextView
  46. android:id="@+id/textView2"
  47. android:layout_width="wrap_content"
  48. android:layout_height="wrap_content"
  49. android:text="张小媛" />
  50. </LinearLayout>
  51. <!-- 第三个tab的布局 -->
  52. <LinearLayout
  53. android:id="@+id/tab3"
  54. android:layout_width="match_parent"
  55. android:layout_height="match_parent" >
  56. <TextView
  57. android:id="@+id/textView3"
  58. android:layout_width="wrap_content"
  59. android:layout_height="wrap_content"
  60. android:text="马贝贝" />
  61. </LinearLayout>
  62. </FrameLayout>
  63. </LinearLayout>
  64. </TabHost>
  65. </LinearLayout>

2、JAVA代码

[java] view plain copy

  1. public class MainActivity extends Activity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. TabHost th=(TabHost)findViewById(R.id.tabhost);
  7. th.setup();            //初始化TabHost容器
  8. //在TabHost创建标签,然后设置:标题/图标/标签页布局
  9. th.addTab(th.newTabSpec("tab1").setIndicator("标签1",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.tab1));
  10. th.addTab(th.newTabSpec("tab2").setIndicator("标签2",null).setContent(R.id.tab2));
  11. th.addTab(th.newTabSpec("tab3").setIndicator("标签3",null).setContent(R.id.tab3));
  12. //上面的null可以为getResources().getDrawable(R.drawable.图片名)设置图标
  13. }
  14. }

效果图:

此例源码地址:http://download.csdn.net/detail/harvic880925/6657611  (不要分,欢迎下载)

方法二:Tab的内容分开:不用继承TabActivity

1、第一个tab的XML布局文件,tab1.xml:

[html] view plain copy

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/LinearLayout01"
  4. android:layout_width="wrap_content"
  5. android:layout_height="wrap_content">
  6. <TextView
  7. android:text="我是标签1的内容喔"
  8. android:id="@+id/TextView01"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content">
  11. </TextView>
  12. </LinearLayout>

2、第二个tab的XML布局文件,tab2.xml:

[html] view plain copy

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/LinearLayout02"
  4. android:layout_width="wrap_content"
  5. android:layout_height="wrap_content">
  6. <TextView android:text="标签2"
  7. android:id="@+id/TextView01"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content" />
  10. </LinearLayout>

3、主布局文件,activity_main.xml:

[html] view plain copy

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="fill_parent"
  3. android:layout_height="fill_parent"
  4. android:orientation="vertical" >
  5. <TabHost
  6. android:id="@+id/tabhost"
  7. android:layout_width="match_parent"
  8. android:layout_height="match_parent" >
  9. <LinearLayout
  10. android:layout_width="match_parent"
  11. android:layout_height="match_parent"
  12. android:orientation="vertical" >
  13. <TabWidget
  14. android:id="@android:id/tabs"
  15. android:layout_width="match_parent"
  16. android:layout_height="wrap_content" >
  17. </TabWidget>
  18. <FrameLayout
  19. android:id="@android:id/tabcontent"
  20. android:layout_width="match_parent"
  21. android:layout_height="match_parent" >
  22. </FrameLayout>
  23. </LinearLayout>
  24. </TabHost>
  25. </LinearLayout>

4、JAVA代码:

[java] view plain copy

  1. public class MainActivity extends Activity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. TabHost m = (TabHost)findViewById(R.id.tabhost);
  7. m.setup();
  8. LayoutInflater i=LayoutInflater.from(this);
  9. i.inflate(R.layout.tab1, m.getTabContentView());
  10. i.inflate(R.layout.tab2, m.getTabContentView());//动态载入XML,而不需要Activity
  11. m.addTab(m.newTabSpec("tab1").setIndicator("标签1").setContent(R.id.LinearLayout01));
  12. m.addTab(m.newTabSpec("tab2").setIndicator("标签2").setContent(R.id.LinearLayout02));
  13. }
  14. }

效果图:

此例源码地址:http://download.csdn.net/detail/harvic880925/6657679   (不要分,欢迎下载)

方法三:继承自TabActivity

1、主布局文件,activity_main.xml:

[html] view plain copy

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <!-- 第一个布局 -->
  6. <LinearLayout
  7. android:id="@+id/view1"
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent" >
  10. <TextView
  11. android:id="@+id/textView1"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:text="张小媛" />
  15. </LinearLayout>
  16. <!-- 第二个布局 -->
  17. <LinearLayout
  18. android:id="@+id/view2"
  19. android:layout_width="match_parent"
  20. android:layout_height="match_parent" >
  21. <TextView
  22. android:id="@+id/textView2"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:text="马贝贝" />
  26. </LinearLayout>
  27. <!-- 第三个布局 -->
  28. <TextView android:id="@+id/view3"
  29. android:background="#00ff00"
  30. android:layout_width="fill_parent"
  31. android:layout_height="fill_parent"
  32. android:text="Tab3"/>
  33. </FrameLayout>

2、JAVA代码:

先将派生自Activity改为TabActivity,然后代码如下:

[java] view plain copy

  1. public class MainActivity extends TabActivity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setTitle("TabDemoActivity");
  6. TabHost tabHost = getTabHost();
  7. LayoutInflater.from(this).inflate(R.layout.activity_main,
  8. tabHost.getTabContentView(), true);
  9. tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("tab1", getResources().getDrawable(R.drawable.ic_launcher))
  10. .setContent(R.id.view1));
  11. tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("tab2")
  12. .setContent(R.id.view2));
  13. tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("tab3")
  14. .setContent(R.id.view3));
  15. //标签切换事件处理,setOnTabChangedListener
  16. tabHost.setOnTabChangedListener(new OnTabChangeListener(){
  17. @Override
  18. public void onTabChanged(String tabId) {
  19. if (tabId.equals("tab1")) {   //第一个标签
  20. }
  21. if (tabId.equals("tab2")) {   //第二个标签
  22. }
  23. if (tabId.equals("tab3")) {   //第三个标签
  24. }
  25. }
  26. });
  27. }
  28. }

效果如下:

此例源码地址:http://download.csdn.net/detail/harvic880925/6657791   (不要分,仅供分享)

四:实现微信底部导航栏

效果:

参看博客:http://blog.csdn.net/wangkuifeng0118/article/details/7745109

 源码地址:http://download.csdn.net/detail/harvic880925/6657767   (不要分,仅供分享)

请大家尊重作者版权,转载请标明出处:http://blog.csdn.net/harvic880925/article/details/17120325

TabHost详解的更多相关文章

  1. Android零基础入门第80节:Intent 属性详解(下)

    上一期学习了Intent的前三个属性,本期接着学习其余四个属性,以及Android系统常用内置组件的启动. 四.Data和Type属性 Data属性通常用于向Action属性提供操作的数据.Data属 ...

  2. Linq之旅:Linq入门详解(Linq to Objects)

    示例代码下载:Linq之旅:Linq入门详解(Linq to Objects) 本博文详细介绍 .NET 3.5 中引入的重要功能:Language Integrated Query(LINQ,语言集 ...

  3. 架构设计:远程调用服务架构设计及zookeeper技术详解(下篇)

    一.下篇开头的废话 终于开写下篇了,这也是我写远程调用框架的第三篇文章,前两篇都被博客园作为[编辑推荐]的文章,很兴奋哦,嘿嘿~~~~,本人是个很臭美的人,一定得要截图为证: 今天是2014年的第一天 ...

  4. EntityFramework Core 1.1 Add、Attach、Update、Remove方法如何高效使用详解

    前言 我比较喜欢安静,大概和我喜欢研究和琢磨技术原因相关吧,刚好到了元旦节,这几天可以好好学习下EF Core,同时在项目当中用到EF Core,借此机会给予比较深入的理解,这里我们只讲解和EF 6. ...

  5. Java 字符串格式化详解

    Java 字符串格式化详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 文中如有纰漏,欢迎大家留言指出. 在 Java 的 String 类中,可以使用 format() 方法 ...

  6. Android Notification 详解(一)——基本操作

    Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...

  7. Android Notification 详解——基本操作

    Android Notification 详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 前几天项目中有用到 Android 通知相关的内容,索性把 Android Notificatio ...

  8. Git初探--笔记整理和Git命令详解

    几个重要的概念 首先先明确几个概念: WorkPlace : 工作区 Index: 暂存区 Repository: 本地仓库/版本库 Remote: 远程仓库 当在Remote(如Github)上面c ...

  9. Drawable实战解析:Android XML shape 标签使用详解(apk瘦身,减少内存好帮手)

    Android XML shape 标签使用详解   一个android开发者肯定懂得使用 xml 定义一个 Drawable,比如定义一个 rect 或者 circle 作为一个 View 的背景. ...

随机推荐

  1. 使用AIDL远程调用服务中的方法

    AIDL:android interface define language(接口定义语言) 作用:方便远程调用其他服务中的方法 注意:安卓四大组件都要在清单文件注册 aidl创建图: AIDL的全称 ...

  2. Android 在资源文件(res/strings.xml)定义一维数组,间接定义二维数组

    经常我们会在资源文件(res/strings.xml)定义字符串,一维数组,那定义二维数组?直接定义二维数组没找到,可以间接定义. 其实很简单,看过用过一次就可以记住了,一维数组估计大家经常用到,但是 ...

  3. windows下基于sublime text3的nodejs环境搭建

    第一步:先安装sublime text3.详细教程可自行百度,这边不具体介绍了. 第二步.安装nodejs插件,有两种方式 第一种方式:直接下载https://github.com/tanepiper ...

  4. quick 关于触摸的问题

    以前遇到一个问题就是,如果触摸层不在最后,会导致触摸失效.这是由于下面添加的层挡住了触摸层,而后添加的层会位于上面,默认是不可点击,点击不可穿透的.所以我们必须将触摸层放置到最上面. Logic.lu ...

  5. 获取当前的时间,转化为char[]格式unix时间戳

    /* 在这个程序当中实现获取当前的unix时间戳 转化为char[] */ #include<stdio.h> #include<stdlib.h> #include<t ...

  6. UITapGestureRecognizer 和 CCMenu点击问题

    当一个scene中同时有UITapGestureRecognizer和CCMenu,点击时不会响应CCMenu,此时必须对UITapGestureRecognizer进行设置 UITapGesture ...

  7. NOIP200107统计单词个数

    NOIP200107统计单词个数 难度级别: A: 编程语言:不限:运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 给出一个长度不超过200的由 ...

  8. git客户端初始化

    安装git客户端:[root@super67 ~]# yum install -y git 配置git信息:[root@super67 ~]# git config --global user.nam ...

  9. opengl绘制正弦曲线

    利用opengl绘制正弦曲线 ,见代码: #include <windows.h> //#include <GLUT/glut.h> #include <GL/glut. ...

  10. Html - 涟漪特效

    这种效果稍加改造非常优雅.并且可以准确的实验触摸聚焦点.缺点是非常消耗内存.娱乐为主吧 js //╠═╬═╬═╬═╬═╬═╬═╬═╬═╬═╣ 涟漪特效 ╠═╬═╬═╬═╬═╬═╬═╬═╬═╬═╣ 涟漪 ...