TabHost创建的2种方式
一.如果是自定义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种方式的更多相关文章
- 于Unity3D动态创建对象和创建Prefab三种方式的原型对象
于Unity3D动态创建对象和创建Prefab三种方式的原型对象 u3d在动态创建的对象,需要使用prefab 和创建时 MonoBehaviour.Instantiate( GameObject o ...
- 并发编程 ~~~ 多进程~~~进程创建的两种方式, 进程pid, 验证进程之间的空间隔离, 进程对象join方法, 进程对象其他属性
一 进程创建的两种方式 from multiprocessing import Process import time def task(name): print(f'{name} is runnin ...
- 黑马vue---56-58、vue组件创建的三种方式
黑马vue---56-58.vue组件创建的三种方式 一.总结 一句话总结: 不论是哪种方式创建出来的组件,组件的 template 属性指向的模板内容,必须有且只能有唯一的一个根元素 1.使用 Vu ...
- JS对象创建的几种方式整理
本文主要介绍了JS对象创建的几种方式 第一种:Object构造函数创建 var Person = new Object(); Person.name = 'Nike'; Person.age = ...
- 【Java EE 学习 52】【Spring学习第四天】【Spring与JDBC】【JdbcTemplate创建的三种方式】【Spring事务管理】【事务中使用dbutils则回滚失败!!!??】
一.JDBC编程特点 静态代码+动态变量=JDBC编程. 静态代码:比如所有的数据库连接池 都实现了DataSource接口,都实现了Connection接口. 动态变量:用户名.密码.连接的数据库. ...
- 对JS关于对象创建的几种方式的整理
最近一直在看JS高级程序设计这本书,有空来梳理一下几种创建对象的方式.话不多说,直接步入正题. 第一种:Object构造函数创建 var Person = new Object();Person.na ...
- java中创建多线程两种方式以及实现接口的优点
多线程创建方式有两种 创建线程的第一种方式.继承Thread类 1.继承Thread类 2.重写Thread类中的run方法--目的将自定义代码存储在run方法.让线程执行3.调用线程的start() ...
- JavaScript对象创建的几种方式
1 工厂模式 1.1 创建 function createFruit(name,colors) { var o = new Object(); o.name = name; o.colors = co ...
- Java线程创建的两种方式
java多线程总结一:线程的两种创建方式及优劣比较 (一)---之创建线程的两种方式 java实现多线程的两种方法的比较
随机推荐
- .netcore持续集成测试篇之搭建内存服务器进行集成测试一
系列目录 在web项目里,我们把每一层的代码的单元测试都通过并不代表程序能正常运行,因为这个过程缺失了http管道,很多时候我们还还需要把项目布在iis环境中或者在vs里启动iis express服务 ...
- 一文了解:Redis过期键删除策略
Redis过期键删除策略 Redis中所有的键都可以设置过期策略,就像是所有的键都可以上"生死簿",上了生死簿的键到时间后阎王就会叉掉这个键.同一时间大量的键过期,阎王就会忙不过来 ...
- c语言实现基本的数据结构(三) 栈
#include <stdio.h> #include <tchar.h> #include <stdlib.h> #define StackSize 5 #def ...
- 最小生成树模板题-----P3366 【模板】最小生成树
题目描述 如题,给出一个无向图,求出最小生成树,如果该图不连通,则输出orz 输入格式 第一行包含两个整数N.M,表示该图共有N个结点和M条无向边.(N<=5000,M<=200000) ...
- git使用WebHook实现自动构建
说明 我们使用git进行版本管理常常会遇到这样的一个需求,希望git push的时候服务器上代码的代码也能自动更新,这次我使用了coding进行示范 一.编写git pull 更新脚本 auth_pu ...
- 实时计算大数据处理的基石-Google Dataflow
此文选自Google大神Tyler Akidau的另一篇文章:Streaming 102: The world beyond batch 欢迎回来!如果您错过了我以前的帖子,Streaming ...
- shiro登录名的获取
登录名的获取:通过的SecurityUtils的shiro import org.apache.shiro.SecurityUtils; //登录用户名 String loginAccount = S ...
- python所有的标准异常类:
异常名称 描述 BaseException 所有异常的基类 SystemExit 解释器请求退出 KeyboardInterrupt 用户中断执行(通常是输入^C) Exception 常规错误的基类 ...
- rabbit - producer的confirm和consumer的ack模式
本篇和大家分享的是关于rabbit的生产和消费方的一些实用的操作:正如文章标题,主要内容如producer的confirm和consumer的ack,这两者使用的模式都是用来保证数据完整性,防止数据丢 ...
- python程序中使用MySQL数据库
目录 python程序中使用MySQL数据库 1 pymysql连接数据库 2 sql 注入 3 增删改查操作 4 pymysql使用总结 python程序中使用MySQL数据库 1.python中使 ...