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实现多线程的两种方法的比较
随机推荐
- 通过Blazor使用C#开发SPA单页面应用程序(2)
今天我们尝试创建一个默认的Blazor应用. 1.安装 .Net Core 3.0需要Visual Studio 2019 的支持. 安装.Net Core 3.0 预览版 SDK版本,注意预览版对应 ...
- Win服务程序编写以及安装一般步骤
Win服务程序编写以及安装一般步骤 Windows服务的优点有:1. 能够自动运行.2. 不要求用户交互.3. 在后台运行.本文将介绍常见服务程序编写的一般步骤以及注意事项. 设计服务程序实例: 创建 ...
- hadoop2.7之作业提交详解(上)
根据wordcount进行分析: import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; impo ...
- lumen 路由访问路径
项目目录/public/index.php/接你设置的路由 比如设置了 $app->get('/test', function () use ($app) { return $app-&g ...
- StudyAndroid.1
目标: 手动创建第一个Activity 开发环境: Android Studio 3.3.1 Build #AI-182.5107.16.33.5264788, built on January 29 ...
- python3.6.6在CentOS7上的安装
Python官网:https://www.python.org/ 下载指定版本的软件包: [root@General data]# yum clean all && yum rep ...
- ngxin安装之yum安装
nginx部署至yum安装 1. 配置nginx所需的YUM源 [root@nginx]# cat /etc/yum.repos.d/yum.repo [nginx-stable] name=ngin ...
- 思科ACS5.8最新搭建教程-亲测可用
1.需要准备的相关软件 ACS5.8安装包:http://ouo.io/MWB0R ACS5.8破解包:http://ouo.io/FaiGgj Centos7:下载地址(破解时需要):http:// ...
- 使用okHttp登录、Md5密码加密
1.使用okHttp3登录 2.Md5密码加密 3.完整代码 4.项目案例 使用okHttp3登录: 使用okHttp3之前要在build.gradle引入okHttp3的依赖(顺便引入解析数据的gs ...
- python 37 同步、异步调用
目录 1. 阻塞与非阻塞 2. 同步与异步 2.1 异步调用 2.2 同步调用 2.3 异步调用回收的第一种方式 3. 异步调用+回调函数 3.1 requests模块 3.2 异步调用回收的第二种方 ...