与TabHost结合使用的组件:

TabWidget:代表选项卡的标签条

TabSpec:代表选项卡的一个Tab页面

TabHost不过一个简单的容器,它提供两个方法来创建、加入选项卡

newTabSpec(String tag):创建选项卡

addTab(TabHOst.TabSpec tabSpec):加入选项卡

步骤:

1.在界面布局文件里定义TabHost组件,并为该组件定义该选项卡的内容

2.Activity继承TabActivity

3.调用TAbActivity的getTabHost()方法获取TabHost对象

4.通过TabHost对象的方法来创建、加入选项卡

布局文件:

<?

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="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" > <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" /> <FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" > <!-- 定义第一个标签页的内容 --> <LinearLayout
android:id="@+id/tab01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tab1"
android:textSize="11pt" />
</LinearLayout>
<!-- 定义第二个标签页的内容 --> <LinearLayout
android:id="@+id/tab02"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tab2"
android:textSize="11pt" />
</LinearLayout>
<!-- 定义第三个标签页的内容 --> <LinearLayout
android:id="@+id/tab03"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:textSize="11pt" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tab3"
android:textSize="11pt" />
</LinearLayout>
</FrameLayout>
</LinearLayout> </TabHost>

TabHost容器内部须要组合两个组件:TabWidget和FrameLayout,当中TabWidget定义选项卡的标题条

Framelayout则用于层叠组合多个选项界面

Activity:

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("01") // 设置标题
.setContent(R.id.tab01); // 设置内容
// 加入第一个标签页
tabHost.addTab(tab1);
TabSpec tab2 = tabHost
.newTabSpec("tab2")
// 在标签标题上放置图标
.setIndicator("02",
getResources().getDrawable(R.drawable.ic_launcher))
.setContent(R.id.tab02);
// 加入第二个标签页
tabHost.addTab(tab2);
TabSpec tab3 = tabHost.newTabSpec("tab3").setIndicator("03")
.setContent(R.id.tab03);
// 加入第三个标签页
tabHost.addTab(tab3);
}
}

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzQ3NjU1Ng==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

有时候选项卡是在下边的,仅仅须要改动布局文件就可以

布局文件:

<?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="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" > <RelativeLayout
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"
android:layout_alignParentBottom="true" /> <FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@android:id/tabs" > <!-- 定义第一个标签页的内容 --> <LinearLayout
android:id="@+id/tab01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tab1"
android:textSize="11pt" />
</LinearLayout>
<!-- 定义第二个标签页的内容 --> <LinearLayout
android:id="@+id/tab02"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tab2"
android:textSize="11pt" />
</LinearLayout>
<!-- 定义第三个标签页的内容 --> <LinearLayout
android:id="@+id/tab03"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:textSize="11pt" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tab3"
android:textSize="11pt" />
</LinearLayout>
</FrameLayout>
</RelativeLayout> </TabHost>

Android---61---TabHost简单使用的更多相关文章

  1. Android底部TabHost API

    今天在项目中遇到了底部TabHost,顺便就写了一个底部TabHost的api继承即可使用非常简单,以下为源代码: 首先是自定义的TabHostActivity,如果要使用该TabHost继承该类即可 ...

  2. Android:PopupWindow简单弹窗改进版

    Android:PopupWindow简单弹窗 继续上一节的内容,改进一下,目标是点击菜单后把菜单收缩回去并且切换内容,我使用的是PopupWindow+RadioGroup public class ...

  3. Android学习Tabhost、gallery、listview、imageswitcher

    Tabhost控件又称分页控件,在很多的开发语言中都存在.它可以拥有多个标签页,每个标签页可以拥有不同的内容.android中,一个标签页可以放 一个view或者一个activity.TabHost是 ...

  4. TabHost 简单用法

    package com.google.tabhost;    import android.app.TabActivity;  import android.os.Bundle;  import an ...

  5. Android项目--tabhost

    所有牵扯到自定义布局的layout中尽量用RelativeLayout 在通讯录中如果像小米手机的UI那就是viewpager,在这里,我们做成静态的.通过tabhost来做. 1.布局 a) 直接用 ...

  6. 深入浅出Tabhost+简单入门Demo

    小伙伴们在手机上逛淘宝的时候,会发现在淘宝的下面有个按钮,分别是首页.微淘.社区.购物车和我的淘宝,点击不同的按钮会跳转到不同的页面,目前小编所接手的这个项目,也需要用到类似这样的功能,小编就发挥网络 ...

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

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

  8. Android中TabHost中实现标签的滚动以及一些TabHost开发的奇怪问题

    最近在使用TabHost的时候遇到了一些奇怪的问题,在这里总结分享备忘一下. 首先说一点TabActivity将会被FragmentActivity所替代,但是本文中却是使用的TabActivity. ...

  9. 12.Android之Tabhost组件学习

    TabHost是整个Tab的容器,TabHost的实现有两种方式: 第一种继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost.各个Tab中的内容在布 ...

  10. android之TabHost(下)

    首先建立res/layout/tab.xml文件 编写代码如下: <?xml version="1.0" encoding="utf-8"?> &l ...

随机推荐

  1. 无序字母对 character

    无序字母对 character 题目描述 给定n个各不相同的无序字母对(区分大小写,无序即字母对中的两个字母可以位置颠倒).请构造一个有n+1个字母的字符串使得每个字母对都在这个字符串中出现. 输入 ...

  2. python获取目录下文件夹名称

    path = '/opt' dirs = os.listdir(path) for dir in dirs: print dir

  3. Python matplotlib 柱状图

    matplotlib是python最著名的绘图库,它提供了一整套和matlab相似的命令API,十分适合交互式地进行制图.而且也可以方便地将它作为绘图控件,嵌入GUI应用程序中.它的文档相当完备,并且 ...

  4. element el-cascader设置默认值

    原文:https://www.jianshu.com/p/b690d7fe6ec0 注意两点就行了 <el-form-item label="AP名称"> <el ...

  5. 洛谷 [P1337] 平衡点

    模拟退火练手 一道模拟退火的好题 结果一定势能最小 与模拟退火思路高度一致 #include <iostream> #include <cstdio> #include < ...

  6. SQL触发器的使用及语法

    原文发布时间为:2010-08-07 -- 来源于本人的百度文章 [由搬家工具导入] ===以下转qsfwy.javaeye.com/blog/424789定义: 何为触发器?在SQL Server里 ...

  7. C#获取二维数组的行数和列数及其多维。。。

    原文发布时间为:2008-11-26 -- 来源于本人的百度文章 [由搬家工具导入] 有一个二维数组sz[,] 怎样获取sz 的行数和列数呢? sz.GetLength(0) 返回第一维的长度(即行数 ...

  8. duilib入门简明教程 -- XML基础类(7) (转)

    原文转自:http://www.cnblogs.com/Alberl/p/3343743.html 现在大家应该对XML描述界面不那么陌生了,那么我们做进一步介绍. 前面的教程我们写了很多代码,为的是 ...

  9. 移动GIS技术在城市信息采集中的应用

    1 引言 随着移动平板电脑和手机(以下简称移动终端)在软硬件上的更新换代,和3G.4G通讯网络的升级,传统测绘和和数据服务方式正在发生巨大变化.以城市中的外业踏勘和信息采集为例,移动终端正成为主要的外 ...

  10. javascript 省市二级联动

    通过遍历二维数组 获取到 二级列表的 每个option 然后onchange事件 获取到省,然后循环遍历该省具有的市并将遍历到的市添加到id为city的选择器中. 获取完需要清空二级列表的内容,不然不 ...