package com.gc.tabhost;
/**
* @author Android将军
*
*
*
* 1、TabHost是一种非常有用的组件,TabHost能够非常方便地在窗体上放置
* 多个标签页,每一个标签页相当于获得了一个与外部容器同样大小的组件
* 摆放区域。通过这样的方式,就能够在一个容器里放置很多其它组件。
* 2、与TabHost结合使用的还有例如以下组件:
* TabWidget:代表选项卡的标签条。
* TabSpec:代表选项卡的一个Tab页面。
* 3、TabHost不过一个简单的容器,它提供了例如以下两个方法来创建、加入
* 选项卡:
* newTabSpec(String tag):创建选项卡。
* addTab(TabHost.TabSpec tabSpec):加入选项卡。
* 4、使用TabHost的一般过程例如以下:
* (1)在界面布局中定义TabHost组件,并为该组件定义该选项卡的内容
* (2)Activity应该继承TabActivity
* (3)调用TabActivity的getTabHost()方法获取TabHost对象
* (4)通过TabHost对象的方法来创建、加入选项卡。
* 5、TabHost容器内部须要组合两个组件:TabWidget和FrameLayout
* ,当中TabWidget定义选项卡的标题条:FrameLayout则用于“层叠”组合多个选项
* 页面。
* 6、注意:
* 在ID的书写时不时开发人员自己书写,TabHost、TabWidget和FrameLayout
* 这三个组件的ID是有要求的:
* TabHost的ID应该为@android:id/tabhost
* TabWidget的ID应该为@android:id/tabs
* FrameLayout的ID应该为@android:id/tabcontent.
* 这三个ID不是我们自定义的,而是引用了Android系统已有的ID。
* 7、最新版本号的Android平台已经不再推荐使用TabActivity,而是推荐使用
* Fragment来取代TabActivity。
*/
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 MainActivity extends TabActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取该Activity里面的TabHost组件
TabHost tabHost=getTabHost();
//创建第一个Tab页
TabSpec tab1=tabHost.newTabSpec("tab1")
.setIndicator("Android将军1")
.setContent(R.id.tab01);
//加入第一个标签页
tabHost.addTab(tab1);
TabSpec tab2=tabHost.newTabSpec("tab2")
.setIndicator("Android将军2",getResources().getDrawable(R.drawable.ic_launcher))
.setContent(R.id.tab02);
//加入第二个标签页
tabHost.addTab(tab2);
TabSpec tab3=tabHost.newTabSpec("tab3").setIndicator("Android将军3")
.setContent(R.id.tab03);
//加入第三个标签页
tabHost.addTab(tab3); } }

对应的xml布局文件为:

<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" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
> </RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Android将军"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="赳赳老秦,共赴国难,秦朝将军"
/>
</LinearLayout>
<!-- 定义第二个标签页的内容 -->
<LinearLayout
android:id="@+id/tab02"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Android将军2"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="赳赳老秦,共赴国难,秦朝将军2"
/>
</LinearLayout>
<!-- 定义第三个标签页的内容 -->
<LinearLayout
android:id="@+id/tab03"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Android将军3"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="赳赳老秦,共赴国难,秦朝将军3"
/>
</LinearLayout>
</FrameLayout> <TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
/>
</RelativeLayout> </LinearLayout> </TabHost>

程序执行效果图为:

转载请注明出处:http://blog.csdn.net/android_jiangjun/article/details/25346627

AndroidUI组件之TabHost的更多相关文章

  1. AndroidUI组件之ImageSwitcher

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/gc_gongchao/article/details/25594669 今天继续AndroidUI组 ...

  2. AndroidUI组件之ActionBar

    有一段时间没有写博文了,发现自己的博文的完整度不是非常好.就拿AndroidUI组件这一块.一直没有更新完.我会尽快更新.好了.不多说了,今天来看一下ActionBar. 依照以往的作风.知识点都以代 ...

  3. AndroidUI组件之ActionBar--基于下拉的导航方式

      在上一篇关于ActionBar的博文中.我们知道了ActionBar是Android3.0的重要更新之中的一个.本篇博文就来写一个开发中经经常使用到的样例.用ActionBar提供基于下拉的导航方 ...

  4. android-UI组件实例大全(六)------ImageView图像视图

    图像视图:ImageView 继承view类,用于在屏幕上显示任何Drawable对象,通常用来显示图片: 这里的话我们介绍一些比较常用的属性: Path 1: android:adjustViewB ...

  5. android组件之TabHost

    一 介绍 1.1 TAB的容器.这个对象包含两个子元素: TabWidget:管理标签(tabs),用户点击来选择一个特定的标签,是它告诉TabHost去切换界面的 FrameLayout:对象显示该 ...

  6. android-UI组件(四):AdapterView及其子类

    http://blog.csdn.net/litianpenghaha/article/details/23270881 AdapterView组件是一组重要的组件,AdapterView本身是一个抽 ...

  7. AndroidUI组件之AdapterViewFilpper

    package com.gc.adapterviewflipperdemo; /** * 功能:自己主动播放的图片库 * @author Android将军 */ /* * 1.AdapterView ...

  8. AndroidUI组件之ListView小技巧

    android:fadingEdge="none"//出去黑影  android:listSelector="@android:color/transparent&quo ...

  9. 第九章 Android-UI组件(2)

    一.图像视图(ImageView) 布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayo ...

随机推荐

  1. 王立平--Program Files (x86)

    window7根据系统.program files(x86) 它是应用程序目录,在64下位系统.为了更好的相容性32位程序,在一些安装32位程序(请注意,有些节目自己是32位),将默认被安装progr ...

  2. 为什么不要在android或者ios上直连mysql或者sqlserver之类的数据库(跳大神)

    很多同学 都有直连这些数据库的想法,假设我说了下面二个问题之后你还想直连,那我也没办法 数据库是一个服务端最重要的部分,也是最脆弱的部分,更是最敏感的部分 假设直连会造成例如以下问题 1.安全问题,你 ...

  3. Android应用开发-小巫CSDN博客client之显示博文具体内容

    Android应用开发-小巫CSDN博客客户端之显示博文具体内容 上篇博文给大家介绍的是怎样嵌入有米广告而且获取收益,本篇博客打算讲讲关于怎样在一个ListView里显示博文的具体信息.这个可能是童鞋 ...

  4. siwft初学(一)

    今天刚開始学习swift语言.首先须要下载xcode6 beta版本号.正式版本号然后会公布.自己学习总结一下,假设有误.请大家指出. 创建project的时候.language选择swift语言. ...

  5. Shiro学习笔记(5)——web集成

    Web集成 shiro配置文件shiroini 界面 webxml最关键 Servlet 測试 基于 Basic 的拦截器身份验证 Web集成 大多数情况.web项目都会集成spring.shiro在 ...

  6. java大全经典的书面采访

    果学网  -专注IT在线www.prismcollege.com 1.面向对象的特征有哪些方面 1.抽象: 抽象就是忽略一个主题中与当前目标无关的那些方面.以便更充分地注意与当前目标有关的方面.抽象并 ...

  7. Quartz.NET学习系列

    Quartz.NET它是一个开源的任务调度引擎,对于周期性任务,持久性任务提供了很好的支持,并且支持持久性.集群等功能. 这是什么对我来说Quartz.NET学习记录: 源代码下载http://yun ...

  8. 修改easyui datebox默认日期格式

    问题描述: 根据jquery easyui datebox demo中给的示例,导入和使用datebox, 发现日期格式为: 6/22/2011, 其他的今天和关闭也是 Today, Close, 对 ...

  9. 安装nodejs搭建ghost个人博客

    记录一下今天在linux下折腾nodejs的过程,相互学习: (不知道为什么用源代码安装,调用npm的时候老是会出现找不到npmlog的 错误,后来换用下了个二进制 来搞,假设那个牛牛知道为什么.希望 ...

  10. 程序猿学英语—In July the English learning summary

    七月的英语学习更加曲折,七月初.查看更多,周六和周日可以保证每天两小时的英语教室学习.周一到周 五一般是上完晚自习九点多来机房学习英语,学习一个小时十点多再回宿舍. 考完试,回家待了五天,不好意思的说 ...