TabHost详解
【转】http://blog.csdn.net/harvic880925/article/details/17120325
前言:今天仔细研究了下TabHost,主要是为了实现微信底部导航栏的功能,最后也给出一个链接,这位老兄用TabHost基本做出来了微信导航栏。
正文
TabHost的实现分为两种,一个是不继承TabActivity,一个是继承自TabActivity;当然了选用继承自TabActivity的话就相对容易一些,下面来看看分别是怎样来实现的吧。
方法一、定义tabhost:不用继承TabActivity
1、布局文件:activity_main.xml
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- tools:context=".MainActivity" >
- <Button
- android:id="@+id/button1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Button" />
- <TabHost
- android:id="@+id/tabhost"
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <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" >
- </TabWidget>
- <FrameLayout
- android:id="@android:id/tabcontent"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <!-- 第一个tab的布局 -->
- <LinearLayout
- android:id="@+id/tab1"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <TextView
- android:id="@+id/textView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="林炳东" />
- </LinearLayout>
- <!-- 第二个tab的布局 -->
- <LinearLayout
- android:id="@+id/tab2"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <TextView
- android:id="@+id/textView2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="张小媛" />
- </LinearLayout>
- <!-- 第三个tab的布局 -->
- <LinearLayout
- android:id="@+id/tab3"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <TextView
- android:id="@+id/textView3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="马贝贝" />
- </LinearLayout>
- </FrameLayout>
- </LinearLayout>
- </TabHost>
- </LinearLayout>
2、JAVA代码
- public class MainActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- TabHost th=(TabHost)findViewById(R.id.tabhost);
- th.setup(); //初始化TabHost容器
- //在TabHost创建标签,然后设置:标题/图标/标签页布局
- th.addTab(th.newTabSpec("tab1").setIndicator("标签1",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.tab1));
- th.addTab(th.newTabSpec("tab2").setIndicator("标签2",null).setContent(R.id.tab2));
- th.addTab(th.newTabSpec("tab3").setIndicator("标签3",null).setContent(R.id.tab3));
- //上面的null可以为getResources().getDrawable(R.drawable.图片名)设置图标
- }
- }
效果图:
此例源码地址:http://download.csdn.net/detail/harvic880925/6657611 (不要分,欢迎下载)
方法二:Tab的内容分开:不用继承TabActivity
1、第一个tab的XML布局文件,tab1.xml:
- <?xml version="1.0" encoding="UTF-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/LinearLayout01"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <TextView
- android:text="我是标签1的内容喔"
- android:id="@+id/TextView01"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- </TextView>
- </LinearLayout>
2、第二个tab的XML布局文件,tab2.xml:
- <?xml version="1.0" encoding="UTF-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/LinearLayout02"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <TextView android:text="标签2"
- android:id="@+id/TextView01"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- </LinearLayout>
3、主布局文件,activity_main.xml:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TabHost
- android:id="@+id/tabhost"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <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" >
- </TabWidget>
- <FrameLayout
- android:id="@android:id/tabcontent"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- </FrameLayout>
- </LinearLayout>
- </TabHost>
- </LinearLayout>
4、JAVA代码:
- public class MainActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- TabHost m = (TabHost)findViewById(R.id.tabhost);
- m.setup();
- LayoutInflater i=LayoutInflater.from(this);
- i.inflate(R.layout.tab1, m.getTabContentView());
- i.inflate(R.layout.tab2, m.getTabContentView());//动态载入XML,而不需要Activity
- m.addTab(m.newTabSpec("tab1").setIndicator("标签1").setContent(R.id.LinearLayout01));
- m.addTab(m.newTabSpec("tab2").setIndicator("标签2").setContent(R.id.LinearLayout02));
- }
- }
效果图:
此例源码地址:http://download.csdn.net/detail/harvic880925/6657679 (不要分,欢迎下载)
方法三:继承自TabActivity
1、主布局文件,activity_main.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <!-- 第一个布局 -->
- <LinearLayout
- android:id="@+id/view1"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <TextView
- android:id="@+id/textView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="张小媛" />
- </LinearLayout>
- <!-- 第二个布局 -->
- <LinearLayout
- android:id="@+id/view2"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <TextView
- android:id="@+id/textView2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="马贝贝" />
- </LinearLayout>
- <!-- 第三个布局 -->
- <TextView android:id="@+id/view3"
- android:background="#00ff00"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:text="Tab3"/>
- </FrameLayout>
2、JAVA代码:
先将派生自Activity改为TabActivity,然后代码如下:
- public class MainActivity extends TabActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setTitle("TabDemoActivity");
- TabHost tabHost = getTabHost();
- LayoutInflater.from(this).inflate(R.layout.activity_main,
- tabHost.getTabContentView(), true);
- tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("tab1", getResources().getDrawable(R.drawable.ic_launcher))
- .setContent(R.id.view1));
- tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("tab2")
- .setContent(R.id.view2));
- tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("tab3")
- .setContent(R.id.view3));
- //标签切换事件处理,setOnTabChangedListener
- tabHost.setOnTabChangedListener(new OnTabChangeListener(){
- @Override
- public void onTabChanged(String tabId) {
- if (tabId.equals("tab1")) { //第一个标签
- }
- if (tabId.equals("tab2")) { //第二个标签
- }
- if (tabId.equals("tab3")) { //第三个标签
- }
- }
- });
- }
- }
效果如下:
此例源码地址: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详解的更多相关文章
- Android零基础入门第80节:Intent 属性详解(下)
上一期学习了Intent的前三个属性,本期接着学习其余四个属性,以及Android系统常用内置组件的启动. 四.Data和Type属性 Data属性通常用于向Action属性提供操作的数据.Data属 ...
- Linq之旅:Linq入门详解(Linq to Objects)
示例代码下载:Linq之旅:Linq入门详解(Linq to Objects) 本博文详细介绍 .NET 3.5 中引入的重要功能:Language Integrated Query(LINQ,语言集 ...
- 架构设计:远程调用服务架构设计及zookeeper技术详解(下篇)
一.下篇开头的废话 终于开写下篇了,这也是我写远程调用框架的第三篇文章,前两篇都被博客园作为[编辑推荐]的文章,很兴奋哦,嘿嘿~~~~,本人是个很臭美的人,一定得要截图为证: 今天是2014年的第一天 ...
- EntityFramework Core 1.1 Add、Attach、Update、Remove方法如何高效使用详解
前言 我比较喜欢安静,大概和我喜欢研究和琢磨技术原因相关吧,刚好到了元旦节,这几天可以好好学习下EF Core,同时在项目当中用到EF Core,借此机会给予比较深入的理解,这里我们只讲解和EF 6. ...
- Java 字符串格式化详解
Java 字符串格式化详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 文中如有纰漏,欢迎大家留言指出. 在 Java 的 String 类中,可以使用 format() 方法 ...
- Android Notification 详解(一)——基本操作
Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...
- Android Notification 详解——基本操作
Android Notification 详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 前几天项目中有用到 Android 通知相关的内容,索性把 Android Notificatio ...
- Git初探--笔记整理和Git命令详解
几个重要的概念 首先先明确几个概念: WorkPlace : 工作区 Index: 暂存区 Repository: 本地仓库/版本库 Remote: 远程仓库 当在Remote(如Github)上面c ...
- Drawable实战解析:Android XML shape 标签使用详解(apk瘦身,减少内存好帮手)
Android XML shape 标签使用详解 一个android开发者肯定懂得使用 xml 定义一个 Drawable,比如定义一个 rect 或者 circle 作为一个 View 的背景. ...
随机推荐
- loj 1009(dfs)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=25835 思路:对每一个连通块将其染色,然后取颜色相同的最多的点,最 ...
- C#环境datagidview添加删除操作
添加 行 dataGridView1.Rows.Add();//添加空行 dataGridView1.Rows.Add("a","b"……);//添加指定列数的 ...
- redis的相关知识
1. 依赖包安装 pom.xml 加入: <!-- redis cache related.....start --> <dependency> <groupId> ...
- SCU3033 Destroying a Painting(最小费用最大流)
题目大概说有一个有n*m个格子的画板,画板上每个格子都有颜色,现在要把所有格子的颜色改成红.绿或者蓝,改变的代价是二者RGB值的曼哈顿距离,还要求红绿蓝格子个数的最大值和最小值要尽可能接近,问最少的代 ...
- CSS创建三角形(小三角)的几种方法
你可以在很多地方看到三角形(小三角):tooltips提示框.下拉菜单.甚至在loading载入动画里.不管你喜欢还是不喜欢,这些小元素对各UI元素之间的联系关系式很重要的. 有一些不同的方法来设计并 ...
- HTMl5/CSS3/Javascript 学习推荐资源
HTMl5/CSS3/Javascript 学习推荐资源 前端的定义应该是数据内容的展示,在国内大家都觉得前端只是HTML+CSS+Javascript,但是实际上与展示有关的都是前端,所以Ruby/ ...
- Delphi中控制Excel(转载)
用Delphi从数据库中取得资料,然后导出到Excel中做成报表是个不错的选择,因为Excel强大的报表功能那可是没话说前提Delphi中要 uses comobj;var Excel:Variant ...
- JS。 问题类型:穷举,迭代。两个关键词:break和continue
问题类型: 穷举:(在不知道什么情况下是我们需要的结果的时候只能够让它一个一个都给走一遍) 百鸡百钱:公鸡1钱,母鸡2钱,小鸡0.5钱. 思路: 公鸡买100只,母鸡,小鸡都是0只: 母鸡50只,公鸡 ...
- MySQL中的datetime与timestamp比较-------转载
原文地址http://database.51cto.com/art/200905/124240.htm MySQL中的datetime与timestamp比较 本文将通过实例比较MySQL中的date ...
- [转]如何:定义和处理 SOAP 标头
本文转自:http://msdn.microsoft.com/zh-cn/library/vstudio/8728chd5(v=vs.100).aspx 本主题专门介绍一项旧有技术.现在应通过使用以下 ...
