Android---61---TabHost简单使用
与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简单使用的更多相关文章
- Android底部TabHost API
今天在项目中遇到了底部TabHost,顺便就写了一个底部TabHost的api继承即可使用非常简单,以下为源代码: 首先是自定义的TabHostActivity,如果要使用该TabHost继承该类即可 ...
- Android:PopupWindow简单弹窗改进版
Android:PopupWindow简单弹窗 继续上一节的内容,改进一下,目标是点击菜单后把菜单收缩回去并且切换内容,我使用的是PopupWindow+RadioGroup public class ...
- Android学习Tabhost、gallery、listview、imageswitcher
Tabhost控件又称分页控件,在很多的开发语言中都存在.它可以拥有多个标签页,每个标签页可以拥有不同的内容.android中,一个标签页可以放 一个view或者一个activity.TabHost是 ...
- TabHost 简单用法
package com.google.tabhost; import android.app.TabActivity; import android.os.Bundle; import an ...
- Android项目--tabhost
所有牵扯到自定义布局的layout中尽量用RelativeLayout 在通讯录中如果像小米手机的UI那就是viewpager,在这里,我们做成静态的.通过tabhost来做. 1.布局 a) 直接用 ...
- 深入浅出Tabhost+简单入门Demo
小伙伴们在手机上逛淘宝的时候,会发现在淘宝的下面有个按钮,分别是首页.微淘.社区.购物车和我的淘宝,点击不同的按钮会跳转到不同的页面,目前小编所接手的这个项目,也需要用到类似这样的功能,小编就发挥网络 ...
- Android的TabHost组件-android的学习之旅(四十)
TabHost简介 虽然,官方建议用Fagment取代TabHost,但是我们还是大概的介绍一下.TabHost是一种非常简单的组件,TabHost可以很方便的在窗口放置多个标签页,每一个标签页相当于 ...
- Android中TabHost中实现标签的滚动以及一些TabHost开发的奇怪问题
最近在使用TabHost的时候遇到了一些奇怪的问题,在这里总结分享备忘一下. 首先说一点TabActivity将会被FragmentActivity所替代,但是本文中却是使用的TabActivity. ...
- 12.Android之Tabhost组件学习
TabHost是整个Tab的容器,TabHost的实现有两种方式: 第一种继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost.各个Tab中的内容在布 ...
- android之TabHost(下)
首先建立res/layout/tab.xml文件 编写代码如下: <?xml version="1.0" encoding="utf-8"?> &l ...
随机推荐
- [HEOI2016/TJOI2016][bzoj4555] 求和 [斯特林数+NTT]
题面 传送门 思路 首先,我们发现这个式子中大部分的项都和$j$有关(尤其是后面的$2^j\ast j!$),所以我们更换一下枚举方式,把这道题的枚举方式变成先$j$再$i$ $f(n)=\sum_{ ...
- [暑假集训--数论]poj2262 Goldbach's Conjecture
In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in whic ...
- SPOJ QTREE Query on a tree VI
You are given a tree (an acyclic undirected connected graph) with n nodes. The tree nodes are number ...
- 在类中写reponse语句
原文发布时间为:2009-06-18 -- 来源于本人的百度文章 [由搬家工具导入] 在类文件中不能直接使用response.write,需要使用 System.Web.HttpContext.Cur ...
- TCP/IP协议详解笔记——ARP协议和RARP协议
ARP:地址解析协议 对于以太网,数据链路层上是根据48bit的以太网地址来确定目的接口,设备驱动程序从不检查IP数据报中的目的IP地址.ARP协议为IP地址到对应的硬件地址之间提供动态映射. 工作过 ...
- django Modelform 使用
前言: 为什么要用form去验证呢? 我们提交的是form表单,在看前端源码时如果检查到POST URL及我们提交的字段,如果没有验证我们是否可以直接POST数据到URL,后台并没有进行校验,直接处理 ...
- linux内核情景分析之信号实现
信号在进程间通信是异步的,每个进程的task_struct结构有一个sig指针,指向一个signal_struct结构 定义如下 struct signal_struct { atomic_t cou ...
- c#随机生成汉字、字母、数字
/// <summary> /// 替换变量 /// </summary> /// <param name="content"></par ...
- HDU5006 Resistance(高斯消元)
给你一个复杂的网路图,然后告诉你s,t,求s,t的等效电阻.方法是设s的电势为1,t的电势为0.然后对于其它的每个点x,满足的是sigma(ux-uy)/R(x,y)(即对每个与x相连的节点y,电势差 ...
- Block为什么使用Copy?
block:本质就是一个object-c对象block:存储位置,可能分为3个地方:代码去,堆区.栈区(ARC情况下会自动拷贝到堆区,因此ARC下只能有两个地方:代码去.堆区)代码区:不访问栈区的变量 ...