TabHost是整个Tab的容器,TabHost的实现有两种方式:

第一种继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost。各个Tab中的内容在布局文件中定义就行了。

第二种方式,不继承TabActivity,在布局文件中定义TabHost即可,但是TabWidget的id必须是@android:id/tabs,FrameLayout的id必须是@android:id/tabcontent。

1)继承TabActivity

如果加载该TabHost画面的类继承TabActivity,并且想通过getTabHost()方法来获取TabHost,getTabWidget()方法获取TabWidget,那么TabHost、TabWidget 、FrameLayout 三者的ID必须是android.R.id.tabhost、android.R.id.tabs、android.R.id.tabcontent, 否则会报运行时异常, 比如提示Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'之类错误

修改主布局activity_main文件:

 <?xml version="1.0" encoding="utf-8"?>
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/back"> <LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"> <TabWidget
android:id="@android:id/tabs"
android:layout_alignParentBottom="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" /> </LinearLayout> </TabHost>

再增加自己定义的子TabHost页面布局文件tabhost1如下:

 <?xml version="1.0" encoding="utf-8"?>
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mytabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"> <LinearLayout
android:id="@+id/tab01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="小明"/> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="小东"/> </LinearLayout> <LinearLayout
android:id="@+id/tab02"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="小红"/> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="小紫"/> </LinearLayout> <LinearLayout
android:id="@+id/tab03"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="小李"/> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="小北"/> </LinearLayout> </TabHost>

最后修改下MainActivity.java代码:

 package com.example.administrator.dialog1;

 import android.os.Bundle;
import android.app.TabActivity;
import android.view.LayoutInflater;
import android.widget.TabHost; public class MainActivity extends TabActivity { TabHost mtabHost = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //调用TabActivity的getTabHost()方法获取TabHost对象
mtabHost = this.getTabHost(); //设置使用TabHost布局
LayoutInflater.from(this).inflate(R.layout.tabhost1, mtabHost.getTabContentView(),true); //添加第一个标签页
mtabHost.addTab(mtabHost.newTabSpec("tab01").setIndicator("已接电话").setContent(R.id.tab01)); //添加第二个标签页,并在其标签上添加一个图片
mtabHost.addTab(mtabHost.newTabSpec("tab02").setIndicator("未接电话",getResources().getDrawable(R.mipmap.ic_launcher)).setContent(R.id.tab02)); //添加第三个标签页
mtabHost.addTab(mtabHost.newTabSpec("tab03").setIndicator("已拨电话").setContent(R.id.tab03)); } }

运行效果:

2) 不继承TabActivity

修改MainActivity.java代码:

 package com.example.administrator.dialog1;

 import android.app.Activity;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabWidget; public class MainActivity extends Activity { TabHost mtabHost = null;
TabWidget tabWidget = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabhost1); //以下三句代码,注意顺序
mtabHost = (TabHost)findViewById(R.id.mytabhost);
mtabHost.setup();
tabWidget = mtabHost.getTabWidget(); //添加第一个标签页
mtabHost.addTab(mtabHost.newTabSpec("tab01").setIndicator("已接电话").setContent(R.id.tab01)); //添加第二个标签页,并在其标签上添加一个图片
mtabHost.addTab(mtabHost.newTabSpec("tab02").setIndicator("未接电话",getResources().getDrawable(R.mipmap.ic_launcher)).setContent(R.id.tab02)); //添加第三个标签页
mtabHost.addTab(mtabHost.newTabSpec("tab03").setIndicator("已拨电话").setContent(R.id.tab03)); mtabHost.setCurrentTab(0);
} }

再修改下tabhost1布局文件:

 <?xml version="1.0" encoding="utf-8"?>
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mytabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"> <TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/> <FrameLayout
android:id="@android:id/tabcontent"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" > <LinearLayout
android:id="@+id/tab01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:text="小明"/> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="60dp"
android:text="小东"/> </LinearLayout> <LinearLayout
android:id="@+id/tab02"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="60dp"
android:text="小红"/> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginBottom="60dp"
android:text="小紫"/> </LinearLayout> <LinearLayout
android:id="@+id/tab03"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_marginTop="60dp"
android:text="小李"/> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_marginBottom="60dp"
android:text="小北"/> </LinearLayout> </FrameLayout> </TabHost>

最终运行效果:

12.Android之Tabhost组件学习的更多相关文章

  1. Android的TabHost组件-android的学习之旅(四十)

    TabHost简介 虽然,官方建议用Fagment取代TabHost,但是我们还是大概的介绍一下.TabHost是一种非常简单的组件,TabHost可以很方便的在窗口放置多个标签页,每一个标签页相当于 ...

  2. Android之EditText组件学习

    一.基础学习 1.Button是TextView的一个子类,所以按钮本身是一个特殊的文本,属性和TextView相似 2.EditText类似html里的input type="text&q ...

  3. Android之TextView组件学习

    一.基础学习 1.findViewById返回View类,该类是所有View组件的父类. 2.子类比父类拥有更多的属性和方法,不过子类找不到的话去父类找 3.marquee:华盖,跑马灯效果:orie ...

  4. Android的四大组件学习

    一.Linearlayout  :  线性布局 1. android:orientation="vertical"     //控件的方向控制,vertical : 垂直布局 ,  ...

  5. 从零開始学android&lt;TabHost标签组件.二十九.&gt;

    TabHost主要特点是能够在一个窗体中显示多组标签栏的内容,在Android系统之中每一个标签栏就称为一个Tab.而包括这多个标签栏的容器就将其称为TabHost.TabHost类的继承结构例如以下 ...

  6. Android应用程序组件Content Provider简要介绍和学习计划

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6946067 在Android系统中,Conte ...

  7. Android之TabHost布局(转)

    1.概念 盛放Tab的容器就是TabHost.TabHost的实现有两种方式: 第一种继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost.各个Tab ...

  8. 转-TabHost组件(一)(实现底部菜单导航)

    http://www.cnblogs.com/lichenwei/p/3974009.html 什么是TabHost? TabHost组件的主要功能是可以进行应用程序分类管理,例如:在用户使用wind ...

  9. 转-TabHost组件(二)(实现底部菜单导航)

    http://www.cnblogs.com/lichenwei/p/3975095.html 上面文章<安卓开发复习笔记——TabHost组件(一)(实现底部菜单导航)>中提到了利用自定 ...

随机推荐

  1. 2014 Super Training #4 E Paint the Grid Reloaded --联通块缩点+BFS

    原题: ZOJ 3781 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 题意: 给一个n*m的X,O构成的格子,对 ...

  2. 数字对 (长乐一中模拟赛day2T2)

    2.数字对 [题目描述] 小H是个善于思考的学生,现在她又在思考一个有关序列的问题. 她的面前浮现出一个长度为n的序列{ai},她想找出一段区间[L, R](1 <= L <= R < ...

  3. Oracle中sequence的使用方法

    在Oracle数据库中,sequence等同于序列号,每次取的时候sequence会自动增加,一般会作用于需要按序列号排序的地方. 1.Create Sequence (注释:你需要有CREATE S ...

  4. java11-5 String类的转换功能

    String的转换功能: byte[] getBytes():把字符串转换为字节数组. char[] toCharArray():把字符串转换为字符数组. static String valueOf( ...

  5. javascript js string.Format()收集

    方案1http://www.cnblogs.com/loogn/archive/2011/06/20/2085165.html String.prototype.format = function(a ...

  6. 转载:关于 WebBrowser只对浏览器外应用程序以及在Internet Explorer 中以提升权限运行的应用程序启用

    我是根据很多大神写的博客,以及我自己在做项目的时候做的关于提升Silverlight 中WebBrowser 提升信任的问题的总结: 1)选中Silverlight主程序,右键“属性”---“Sliv ...

  7. C#定制并发送HTML邮件

    HTML格式的邮件能够使用所有html/css使得邮件更丰富,比如现在很多newsletter 都是使用的html邮件. 今天试了一下,如何把图片嵌入到html中呢? 方法一,你的图片host到了in ...

  8. NET Memory Profiler 跟踪.net 应用内存

    NET Memory Profiler 跟踪.net 应用内存 用 .NET Memory Profiler 跟踪.net 应用内存使用情况--基本应用篇 作者:肖波      .net 框架号称永远 ...

  9. Android Studio介绍

    参考资料:http://www.cnblogs.com/smyhvae/p/4390905.html 第一次使用Android Studio时你应该知道的一切配置   as是一种不错的开发Androi ...

  10. CSS 清除浮动的四种方法

    在实际项目中,我们经常会用到float属性来对页面进行布局.当使用float时,意味着该元素已经脱离了文档流,相当于浮于文档之上,不占据空间.但是针对兄弟元素为文字内容时,会占据一定空间,从而产生文字 ...