一.如果是自定义TabHost步骤如下

1.必须给tabHost跟标签设置一个android:id="@android:id/tabhost">

2.必须创建TabWidget子节点,并且必须设定android:id="@android:id/tabs">该标签一般放到一个线性布局当中

3.必须创建FrameLayout节点,用于显示每个TabWidget标签的内容,且id必须是android:id="@android:id/tabcontent">

在代码中使用TabHost与TabActivyt比较相似,不同的只有开始的两个步骤,其具体步骤如下:

(1)使用setContentView()方法显示界面。

(2)TabHost对象获得并设置。

(3)创建并设置TabSpec对象。

(4)向TabHost中添加TabSpec完成标签页的使用。

与使用TabActivity相比较不难发现,自定义TabHost时不需要继承TabActivity了,只需要简单继承Activity就可以了,这无疑给我们编程提供了更大的自由发挥的空间。因为我们知道继承虽然会给我们的编程带来很大程度的方便,但也同样带来了很多的条条框框的限制。

案例:

public class MainActivity extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.tab); TabHost tab = getTabHost();
// 也可以不要setContentView(R.layout.tab),通过下面方式也许
// LayoutInflater.from(this).inflate(R.layout.tab,
// tab.getTabContentView(), true); TabSpec tab1 = tab.newTabSpec("tab1");
TabSpec tab2 = tab.newTabSpec("tab2");
TabSpec tab3 = tab.newTabSpec("tab3"); tab.addTab(tab1.setContent(R.id.tab1).setIndicator("第一个tab"));
tab.addTab(tab2.setContent(R.id.tab2).setIndicator("第2个tab"));
tab.addTab(tab3.setContent(R.id.tab3).setIndicator("第3个tab")); }
}
<?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" > <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" > <TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</TabWidget> <FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" > <LinearLayout
android:id="@+id/tab1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="第1个tab" />
</LinearLayout> <LinearLayout
android:id="@+id/tab2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="第2个tab" />
</LinearLayout> <LinearLayout
android:id="@+id/tab3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="第3个tab" />
</LinearLayout>
</FrameLayout>
</LinearLayout> </TabHost>

二.不使用Tabhost作为跟标签

<?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"
android:orientation="vertical" > <LinearLayout
android:id="@+id/sll01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="vertical" > <ImageView
android:id="@+id/v1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/t1" >
</ImageView>
</LinearLayout> <LinearLayout
android:id="@+id/sll02"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="vertical" > <ImageView
android:id="@+id/v2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/t2" >
</ImageView>
</LinearLayout> <LinearLayout
android:id="@+id/sll03"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="vertical" > <ImageView
android:id="@+id/v3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/t3" >
</ImageView>
</LinearLayout> </FrameLayout>
public class MainActivity2 extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); TabHost host = getTabHost(); LayoutInflater.from(this).inflate(R.layout.tab2,
host.getTabContentView(), true); host.addTab(host.newTabSpec("tab1").setContent(R.id.sll01)
.setIndicator("第一个", getResources().getDrawable(R.drawable.t1))); host.addTab(host.newTabSpec("tab2").setContent(R.id.sll02)
.setIndicator("第二个", getResources().getDrawable(R.drawable.t2)));
host.addTab(host.newTabSpec("tab3").setContent(R.id.sll03)
.setIndicator("第三个", getResources().getDrawable(R.drawable.t3))); setContentView(host);
}
}

TabHost创建的2种方式的更多相关文章

  1. 于Unity3D动态创建对象和创建Prefab三种方式的原型对象

    于Unity3D动态创建对象和创建Prefab三种方式的原型对象 u3d在动态创建的对象,需要使用prefab 和创建时 MonoBehaviour.Instantiate( GameObject o ...

  2. 并发编程 ~~~ 多进程~~~进程创建的两种方式, 进程pid, 验证进程之间的空间隔离, 进程对象join方法, 进程对象其他属性

    一 进程创建的两种方式 from multiprocessing import Process import time def task(name): print(f'{name} is runnin ...

  3. 黑马vue---56-58、vue组件创建的三种方式

    黑马vue---56-58.vue组件创建的三种方式 一.总结 一句话总结: 不论是哪种方式创建出来的组件,组件的 template 属性指向的模板内容,必须有且只能有唯一的一个根元素 1.使用 Vu ...

  4. JS对象创建的几种方式整理

    ​ 本文主要介绍了JS对象创建的几种方式 第一种:Object构造函数创建 var Person = new Object(); Person.name = 'Nike'; Person.age = ...

  5. 【Java EE 学习 52】【Spring学习第四天】【Spring与JDBC】【JdbcTemplate创建的三种方式】【Spring事务管理】【事务中使用dbutils则回滚失败!!!??】

    一.JDBC编程特点 静态代码+动态变量=JDBC编程. 静态代码:比如所有的数据库连接池 都实现了DataSource接口,都实现了Connection接口. 动态变量:用户名.密码.连接的数据库. ...

  6. 对JS关于对象创建的几种方式的整理

    最近一直在看JS高级程序设计这本书,有空来梳理一下几种创建对象的方式.话不多说,直接步入正题. 第一种:Object构造函数创建 var Person = new Object();Person.na ...

  7. java中创建多线程两种方式以及实现接口的优点

    多线程创建方式有两种 创建线程的第一种方式.继承Thread类 1.继承Thread类 2.重写Thread类中的run方法--目的将自定义代码存储在run方法.让线程执行3.调用线程的start() ...

  8. JavaScript对象创建的几种方式

    1 工厂模式 1.1 创建 function createFruit(name,colors) { var o = new Object(); o.name = name; o.colors = co ...

  9. Java线程创建的两种方式

    java多线程总结一:线程的两种创建方式及优劣比较 (一)---之创建线程的两种方式 java实现多线程的两种方法的比较

随机推荐

  1. jQuery中的append中含有onClick的问题

    在jQuery中,当append中含有onClick时,点击事件无效果.需要在append完之后再额外绑定点击事件.

  2. Android Pie 私人 DNS 使用教程

    本文首发于:微信公众号「运维之美」,公众号 ID:Hi-Linux. ​「运维之美」是一个有情怀.有态度,专注于 Linux 运维相关技术文章分享的公众号.公众号致力于为广大运维工作者分享各类技术文章 ...

  3. Shell脚本书写规范

    在日常的运维工作中,Shell脚本肯定是必不可少的工作内容.为方便问题排查.脚本执行历史问题追踪.方便大家共同维护,从网上搜罗结合以往的经验教训拟定以下Bash脚本书写规范.欢迎各位同学指正或补充. ...

  4. 大白话5分钟带你走进人工智能-第35节神经网络之sklearn中的MLP实战(3)

    本节的话我们开始讲解sklearn里面的实战: 先看下代码: from sklearn.neural_network import MLPClassifier X = [[0, 0], [1, 1]] ...

  5. jmeter之beanshell使用

    beanshell官网:http://www.BeanShell.org/ 一.beanshell介绍 是一种完全符合Java语法规范的轻量级的脚本语言: 相当于一个小巧免费嵌入式的Java源代码解释 ...

  6. Windos 上逆天又好用的软件有哪些?

    谷歌浏览器 Chrome 浏览器是大名鼎鼎的科技公司谷歌开发的一款浏览器,国内的360浏览器等大多都是基于谷歌开源出的浏览器内核,然后给他穿了一层360的衣服.至于性能和启动速度上来讲,我个人觉得Ch ...

  7. PrintWriter out = response.getWriter();乱码解决

     resopnse  request的乱码问题 今天在改项目时,发现这个简单又容易忽视在问题.说起这个问题,比较简单,但也比较容易忽视.下面就具体讲讲这个问什么会出现乱码问题. request乱码指的 ...

  8. 回顾js中的cookie/localstorage

    1.首先简单总结下cookie cookie:可以做会话跟踪 特点:      1.大小限制(不能超过4k)      2.每个域下cookie不能超过50个      3.有效期(和设定时间有关), ...

  9. C笔记_常用快捷键

    1.第一部分 Ctrl + up/down 以光标所在行为中心上下移动文本: Ctrl + left/right 左右跳过一个单词或符号: Ctrl + end 跳至文本末尾: Ctrl + dele ...

  10. 设计模式(C#)——02工厂模式

    推荐阅读:  我的CSDN  我的博客园  QQ群:704621321       在简单工厂模式中讲到简单工厂模式的缺点:难以扩展,一旦添加新运算就必须修改简单工厂方法.       工厂方法模式: ...